This code below aim to listing all folders in one directory and get size of each in byte. It uses only pathlib module for that.
#encoding:utf-8
from pathlib import Path
# # # # # Functions # # # # #
def run() :
print("Hello world")
def write_into_file(folder, file_name:str="stats_dossiers.txt") -> None :
"""
__Summary__ : this function write into file_name the name of each folder in directory targeted and weight associated in byte.
folder : directory targeted for analysis.
file_name : name of file in .txt format, which will contain informations.
"""
# Check if file_name is .txt format :
if file_name.endswith(".txt") :
# Write into a file name of folder and its weight.
new_file = Path(folder, file_name)
# Check if file already exists.
if new_file in folder.iterdir() :
print(f"The file {new_file} already exists. Choose another filename or delete existing file.")
print("End of script.")
else :
stats_folder = open(new_file, "w", encoding="utf-8")
print(f"The file {new_file} was created with success")
stats_folder.write("Name of folder ; Weight (Byte) \n")
for file in folder.iterdir() :
name = str(file)
size = file.stat().st_size
stats_folder.write(f"{name} ; {size} \n")
stats_folder.close()
else :
print(f"{file_name} is not a .txt format. Choose another file in .txt format.")
# # # # # GLOBAL VARIABLES # # # # #
# Define directory to analyze.
FOLDER = Path.cwd()
# Listing all folders in current directory.
if __name__ == "__main__" :
run()
write_into_file(folder=FOLDER)
Could you help me in order to identify bad practices in this code? For example: bad overlaps of if/else, break function into many functions instead of using many if/else statement ...
iterdiriterates over the whole contents of the directory). \$\endgroup\$.txtis not a format, it's just a file extension sometimes used to denote "plain text" file content. You may enforce use of that extension, but it's not always desirable: for example, I often use extension-less files, especially when they are short-lived likecd "$(mktemp -d)"; do_smth >./out. It's also sometimes convenient to use other extensions (.out,.log,.temp,.tmpare the few that come to my mind) - why do you need an explicit check to reject those? \$\endgroup\$#encoding:utf-8is weird. If you think ofcodingmagic pragma, it is typically spelled as# -*- coding: utf-8 -*-, and is not needed for many years - utf8 is the default source encoding since 3.0 (circa 2007). \$\endgroup\$