Compare commits

...

2 Commits

Author SHA1 Message Date
perro tuerto 041dc9c871 Ejercicio 10c 2023-06-12 17:23:17 -07:00
perro tuerto ee44448de9 Ejercicio 10b 2023-06-12 16:41:28 -07:00
7 changed files with 80 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: eliminará el archivo

View File

@ -2,3 +2,49 @@
Project: Filling in the Gaps
Page 248
"""
import re
import sys
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(description="Renumeración de archivos")
parser.add_argument("-p", "--pattern", type=str, default="*.*")
parser.add_argument("-i", "--input", type=Path, default=Path.cwd())
parser.add_argument("--add-gap", type=int, default=0)
args = parser.parse_args()
files = []
if not args.input.exists():
print("ERROR: el input no existe")
sys.exit(1)
for path in list(args.input.glob(args.pattern)):
try:
index = int(re.sub(r"^\D+", "", path.stem))
except Exception:
index = 0
files.append({"index": index, "path": path})
if len(files) == 0:
print(f"No se encontraron archivo con el patrón '{args.pattern}'")
sys.exit()
files = sorted(files, key=lambda f: f["index"])
biggest = str(files[-1]["index"])
curr_index = 1
for file in files:
if args.add_gap == curr_index:
curr_index += 1
num = str(curr_index)
zeros = "0" * (len(biggest) - len(num))
index = f"{zeros}{num}"
curr_index += 1
if re.search(r"\d+", file["path"].stem):
parts = re.split(r"\d+", file["path"].stem)
file_name = parts[0] + index + "".join(parts[1:])
else:
file_name = file["path"].stem + index
new_path = file["path"].parent / f"{file_name}{file['path'].suffix}"
print(f"{file['path']} => {new_path}")
# file["path"].rename(new_path) # PELIGRO: renombrará el archivo

0
exercises/ex10/test.txt Normal file
View File

View File

View File

0
exercises/ex10/test1.txt Normal file
View File

View File