From 6d37344f7c5bddb087f77fdd98d126f9f4f5fd90 Mon Sep 17 00:00:00 2001 From: perro Date: Tue, 7 Mar 2023 10:35:36 -0800 Subject: [PATCH] Ejercicio 12b --- exercises/ex12/rm | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/exercises/ex12/rm b/exercises/ex12/rm index e8b601d..249eba2 100755 --- a/exercises/ex12/rm +++ b/exercises/ex12/rm @@ -1,12 +1,27 @@ #!/usr/bin/env python -# Exercise 12a -# ls command +# Exercise 12b +# rm command +import sys +import shutil import argparse from pathlib import Path parser = argparse.ArgumentParser() -parser.add_argument("paths", default=Path().cwd(), nargs="*", type=Path) +parser.add_argument("paths", nargs="+", type=Path) parser.add_argument("-r", "--recursive", action="store_true") args = parser.parse_args() result = "" +error = False + +for path in args.paths: + if path.is_dir() and args.recursive is False: + print(f"rm: no se puede borrar '{path}': Es un directorio") + error = True + elif path.is_file(): + path.unlink() + else: + shutil.rmtree(path) + +if error: + sys.exit(1)