Added invalid directory exceptions to fileList.py

This commit is contained in:
tiagorg 2022-11-04 20:02:20 +00:00
parent c0459353c2
commit 6cd9101be0
Signed by untrusted user who does not match committer: TiagoRG
GPG Key ID: DFCD48E3F420DB42
1 changed files with 25 additions and 16 deletions

View File

@ -8,22 +8,31 @@ def main():
def printFilesSize(path): def printFilesSize(path):
print(f'|{"-"*78}|') try:
print(f'| {"File":<63} {"Size":>12} |\n|{"-"*78}|') directory = os.listdir(path)
for file in os.listdir(path): except FileNotFoundError:
base_size = os.stat(f'{path}/{file}').st_size print(f"[Error] Unable to find that directory: '{os.path.abspath(path)}'")
if os.path.isdir(f'{path}/{file}'): exit(1)
size = "-Directory-" except NotADirectoryError:
elif base_size < 1024: print(f"[Error] Path is not a directory: '{os.path.abspath(path)}'")
size = str(base_size) + ' B' exit(1)
elif base_size < 1024 ** 2: else:
size = f'{base_size // 1024}.{str(base_size % 1024)[0]} KB' print(f'|{"-"*78}|')
elif base_size < 1024 ** 3: print(f'| {"File":<63} {"Size":>12} |\n|{"-"*78}|')
size = f'{base_size // (1024**2)}.{str(base_size % (1024**2))[0]} MB' for file in directory:
else: base_size = os.stat(f'{path}/{file}').st_size
size = f'{base_size // (1024**3)}.{str(base_size % (1024**3))[0]} GB' if os.path.isdir(f'{path}/{file}'):
print(f'| {file:<63} {size:>12} |') size = "-Directory-"
print(f'|{"-"*78}|') elif base_size < 1024:
size = str(base_size) + ' B'
elif base_size < 1024 ** 2:
size = f'{base_size // 1024}.{str(base_size % 1024)[0]} KB'
elif base_size < 1024 ** 3:
size = f'{base_size // (1024**2)}.{str(base_size % (1024**2))[0]} MB'
else:
size = f'{base_size // (1024**3)}.{str(base_size % (1024**3))[0]} GB'
print(f'| {file:<63} {size:>12} |')
print(f'|{"-"*78}|')
if __name__ == "__main__": if __name__ == "__main__":