Borrar ficheros *.out y *.err de simulaciones de dinámica molecular cuando hayan terminado.
Agiliza tiempo de trabajo cuando hayas terminado uma serie de simulaciones de MD, cada una en una subcarpeta dentro de una ruta de ejecución. Si mi simulación dura 200 ns, deben generarse 2001 ficheros *.sim, y cuando esto haya ocurrido no quiero perder el tiempo en repasar carpeta por carpeta revisando que existen los 2001 ficheros *.sim. Una vez terminada la simulación quiero borrar los ficheros *.err y *.out y renombrar cada subcarpeta añadiendo a su nombre inicial la terminación "_ok". El siguiente script hace todo eso.
#!/usr/bin/env python3
import sys
from pathlib import Path
BASE_PATH = Path("/SCRATCH/CTS164/jant.encinar.umh.es/runMD2")
EXPECTED_SIM_COUNT = 2001
def process_directories(base_path: Path) -> None:
if not base_path.exists():
print(f"ERROR: Base path does not exist: {base_path}")
sys.exit(1)
if not base_path.is_dir():
print(f"ERROR: Base path is not a directory: {base_path}")
sys.exit(1)
print(f"Scanning directory: {base_path}")
print("-" * 60)
for subdir in base_path.iterdir():
if not subdir.is_dir():
continue
dirname = subdir.name
if dirname.endswith("_fin") or dirname.endswith("_ok"):
continue
sim_files = list(subdir.glob("*.sim"))
sim_count = len(sim_files)
print(f"Checking {dirname} -> {sim_count} .sim files")
if sim_count == EXPECTED_SIM_COUNT:
print(f"Condition met for {dirname}")
# Delete .err files
for err_file in subdir.glob("*.err"):
try:
err_file.unlink()
print(f"Deleted {err_file.name}")
except Exception as e:
print(f"Error deleting {err_file.name}: {e}")
# Delete .out files
for out_file in subdir.glob("*.out"):
try:
out_file.unlink()
print(f"Deleted {out_file.name}")
except Exception as e:
print(f"Error deleting {out_file.name}: {e}")
# Rename directory
new_path = subdir.with_name(dirname + "_ok")
if new_path.exists():
print(f"WARNING: Target directory already exists: {new_path}")
continue
try:
subdir.rename(new_path)
print(f"Renamed to {new_path.name}")
except Exception as e:
print(f"Error renaming {dirname}: {e}")
else:
print(f"Condition not met for {dirname}")
print("-" * 60)
print("Finished.")
if __name__ == "__main__":
process_directories(BASE_PATH)