2022-10-28 19:13:43 +00:00
|
|
|
import os
|
|
|
|
from sys import argv
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
path = argv[1] if len(argv) > 1 else os.getcwd()
|
|
|
|
printFilesSize(path)
|
|
|
|
|
|
|
|
|
|
|
|
def printFilesSize(path):
|
2022-11-04 20:02:20 +00:00
|
|
|
try:
|
|
|
|
directory = os.listdir(path)
|
|
|
|
except FileNotFoundError:
|
|
|
|
print(f"[Error] Unable to find that directory: '{os.path.abspath(path)}'")
|
|
|
|
exit(1)
|
|
|
|
except NotADirectoryError:
|
|
|
|
print(f"[Error] Path is not a directory: '{os.path.abspath(path)}'")
|
|
|
|
exit(1)
|
|
|
|
else:
|
2023-05-16 20:18:38 +00:00
|
|
|
print(f'|{"-" * 78}|')
|
|
|
|
print(f'| {"File":<63} {"Size":>12} |\n|{"-" * 78}|')
|
2022-11-04 20:02:20 +00:00
|
|
|
for file in directory:
|
|
|
|
base_size = os.stat(f'{path}/{file}').st_size
|
|
|
|
if os.path.isdir(f'{path}/{file}'):
|
|
|
|
size = "-Directory-"
|
|
|
|
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:
|
2023-05-16 20:18:38 +00:00
|
|
|
size = f'{base_size // (1024 ** 2)}.{str(base_size % (1024 ** 2))[0]} MB'
|
2022-11-04 20:02:20 +00:00
|
|
|
else:
|
2023-05-16 20:18:38 +00:00
|
|
|
size = f'{base_size // (1024 ** 3)}.{str(base_size % (1024 ** 3))[0]} GB'
|
2022-11-04 20:02:20 +00:00
|
|
|
print(f'| {file:<63} {size:>12} |')
|
2023-05-16 20:18:38 +00:00
|
|
|
print(f'|{"-" * 78}|')
|
2022-10-28 19:13:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|