Ejercicio 10b

This commit is contained in:
perro tuerto 2023-06-12 16:41:28 -07:00
parent b9831397ef
commit ee44448de9
1 changed files with 34 additions and 0 deletions

View File

@ -2,3 +2,37 @@
Project: Deleting Unneeded Files
Page 247
"""
import sys
from pathlib import Path
def get_path():
if len(sys.argv) != 2:
print("ERROR: solo un argumento es necesario")
sys.exit(1)
path = Path(sys.argv[1])
if not path.is_dir():
print(f"ERROR: '{path}' no es una ruta a un directorio")
sys.exit(1)
return path.resolve()
root = get_path()
size_max = 100
big_files = []
for path in list(root.glob("**/*")):
size_mb = round(path.lstat().st_size / (1024*1024), 2)
if size_mb >= size_max:
big_files.append({"size": size_mb, "path": path})
print(f"Mostrando archivos >= a {size_max}MB:")
# print(f"Eliminando archivos >= a {size_max}MB:")
big_files = sorted(big_files, reverse=True, key=lambda f: f['size'])
biggest = str(format(big_files[0]["size"], ","))
for file in big_files:
size = str(format(file["size"], ","))
space = " " * (len(biggest) - len(size))
print(f"[{space}{size}] {file['path']}")
# file["path"].unlink() # PELIGRO: si se descomenta eliminará el archivo