uaveiro-leci/1ano/fp/aula06/fileList.py

29 lines
821 B
Python

import os
from sys import argv
def main():
path = argv[1] if len(argv) > 1 else os.getcwd()
printFilesSize(path)
def printFilesSize(path):
print(f'|{"-"*35}|')
print(f'| {"File":<20} {"Size":>12} |\n|{"-"*35}|')
for file in os.listdir(path):
base_size = os.stat(f'{path}/{file}').st_size
if 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:<20} {size:>12} |')
print(f'|{"-"*35}|')
if __name__ == "__main__":
main()