2023-10-21 13:05:20 +00:00
#!/bin/python3.11
import os
from sys import argv
2023-10-21 14:18:45 +00:00
class Tcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
DARKERGREEN = '\033[32m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
2023-10-21 13:05:20 +00:00
def main():
2023-10-21 14:18:45 +00:00
if len(argv) > 2:
print(f"{Tcolors.FAIL}[Error] Too many arguments.{Tcolors.ENDC}")
exit(1)
path = argv[1] if len(argv) > 1 and argv[1] != '-r' else os.getcwd()
reverse = True if '-r' in argv else False
print_files_size(path, reverse)
2023-10-21 13:05:20 +00:00
2023-10-21 14:18:45 +00:00
def print_files_size(path, reverse):
files = []
2023-10-21 13:05:20 +00:00
try:
directory = os.listdir(path)
except FileNotFoundError:
2023-10-21 14:18:45 +00:00
print(f"{Tcolors.FAIL}[Error] Unable to find that directory: '{os.path.abspath(path)}'{Tcolors.ENDC}")
2023-10-21 13:05:20 +00:00
exit(1)
except NotADirectoryError:
2023-10-21 14:18:45 +00:00
print(f"{Tcolors.FAIL}[Error] Path is not a directory: '{os.path.abspath(path)}'{Tcolors.ENDC}")
exit(1)
except PermissionError:
print(f"{Tcolors.FAIL}[Error] Permission denied for: '{os.path.abspath(path)}'{Tcolors.ENDC}")
2023-10-21 13:05:20 +00:00
exit(1)
else:
2023-10-21 14:18:45 +00:00
print(f"{Tcolors.HEADER}Scanning....{Tcolors.ENDC}")
2023-10-21 13:05:20 +00:00
total_size = 0
2023-10-21 14:18:45 +00:00
for file in directory:
try:
size = get_dir_size(f'{path}/{file}') if os.path.isdir(f'{path}/{file}') else os.stat(f'{path}/{file}').st_size
except PermissionError:
size = 0
except FileNotFoundError:
size = 0
except OSError:
size = 0
2023-10-21 13:05:20 +00:00
total_size += size
2023-10-21 14:18:45 +00:00
compacted_size, color = compact_size(size)
2023-10-21 13:05:20 +00:00
if os.path.isdir(f'{path}/{file}'):
2023-10-21 14:18:45 +00:00
compacted_size = f"{'<DIR>':<5} {compacted_size:>12}"
files.append((file, size, compacted_size, color))
files = sorted(files, key=lambda x: x[1], reverse=reverse)
print(f"{Tcolors.OKBLUE}|{'-'*237}|\n| {Tcolors.OKCYAN}ID {Tcolors.OKBLUE}| {Tcolors.OKCYAN}{'File':<200} {'Size':>24} {Tcolors.OKBLUE}| {Tcolors.OKCYAN}ID {Tcolors.OKBLUE}|\n|{'-'*237}|")
for index, (file, size, compacted_size, color) in enumerate(files, 1):
print(f"|{color} {index:<2} {Tcolors.OKBLUE}|{color} {file:<184} {compacted_size:>40} {Tcolors.OKBLUE}|{color} {index:>2} {Tcolors.OKBLUE}|")
total_size, color = compact_size(total_size)
print(f"|{'-'*237}|\n| {f'{len(directory)} files/directories found in {os.path.abspath(path)}':<162} {f'Total size:{color} {total_size}':>77} {Tcolors.OKBLUE}|\n|{'-'*237}|{Tcolors.ENDC}")
2023-10-21 13:05:20 +00:00
def get_dir_size(path):
total = 0
try:
for file in os.listdir(path):
if os.path.isfile(f'{path}/{file}'):
total += os.stat(f'{path}/{file}').st_size
else:
total += get_dir_size(f'{path}/{file}')
except PermissionError:
return 0
except FileNotFoundError:
return 0
except NotADirectoryError:
return 0
except OSError:
return 0
return total
def compact_size(size):
if size < 1024:
size = str(size) + ' B'
2023-10-21 14:18:45 +00:00
color = Tcolors.DARKERGREEN
2023-10-21 13:05:20 +00:00
elif size < 1048576:
size = str(round(size / 1024, 2)) + ' KB'
2023-10-21 14:18:45 +00:00
color = Tcolors.DARKERGREEN
2023-10-21 13:05:20 +00:00
elif size < 1073741824:
size = str(round(size / 1048576, 2)) + ' MB'
2023-10-21 14:18:45 +00:00
color = Tcolors.DARKERGREEN
if int(size.split('.')[0]) >= 50:
color = Tcolors.OKGREEN
if int(size.split('.')[0]) >= 500:
color = Tcolors.WARNING
2023-10-21 13:05:20 +00:00
elif size < 1099511627776:
size = str(round(size / 1073741824, 2)) + ' GB'
2023-10-21 14:18:45 +00:00
if int(size.split('.')[0]) >= 10:
color = Tcolors.FAIL
else:
color = Tcolors.WARNING
2023-10-21 13:05:20 +00:00
elif size < 1125899906842624:
size = str(round(size / 1099511627776, 2)) + ' TB'
2023-10-21 14:18:45 +00:00
color = Tcolors.FAIL
return size, color
2023-10-21 13:05:20 +00:00
if __name__ == "__main__":
main()