diff --git a/exercises/ex12/rm b/exercises/ex12/rm index 249eba2..cc533c4 100755 --- a/exercises/ex12/rm +++ b/exercises/ex12/rm @@ -11,7 +11,6 @@ parser = argparse.ArgumentParser() 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: diff --git a/exercises/ex12/rmdir b/exercises/ex12/rmdir new file mode 100755 index 0000000..c8ee292 --- /dev/null +++ b/exercises/ex12/rmdir @@ -0,0 +1,31 @@ +#!/usr/bin/env python +# Exercise 12c +# rmdir command + +import sys +import argparse +from pathlib import Path + +parser = argparse.ArgumentParser() +parser.add_argument("paths", nargs="+", type=Path) +parser.add_argument("-p", "--parents", action="store_true") +args = parser.parse_args() +error = False + +for path in args.paths: + if path.is_file(): + print(f"rmdir: fallo al borrar '{path}': No es un directorio") + error = True + elif len(list(path.glob("*"))) > 0: + print(f"rmdir: fallo al borrar '{path}': El directorio no está vacío") + error = True + else: + if args.parents is True: + while len(path.parts) > 0: + path.rmdir() + path = path.parent + else: + path.rmdir() + +if error: + sys.exit(1)