From bf58092040735c7dbc9cdec2074ec0d5bd6b6f8b Mon Sep 17 00:00:00 2001 From: perro Date: Mon, 13 Mar 2023 11:46:08 -0700 Subject: [PATCH] Ejercicio 12d --- exercises/ex12/mkdir | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 exercises/ex12/mkdir diff --git a/exercises/ex12/mkdir b/exercises/ex12/mkdir new file mode 100755 index 0000000..faca6e7 --- /dev/null +++ b/exercises/ex12/mkdir @@ -0,0 +1,39 @@ +#!/usr/bin/env python +# Exercise 12d +# mkdir 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 + + +def err_msg(key, path): + msg = { + "exists": "»: El fichero ya existe", + "no_exists": "»: No existe el fichero o el directorio", + } + return f"mkdir: no se puede crear el directorio «{path}{msg[key]}" + + +for path in args.paths: + if path.exists() and not args.parents: + error = True + print(err_msg("exists", path)) + if not path.parent.exists() and not args.parents: + error = True + print(err_msg("no_exists", path)) + else: + path = path.resolve() + curr_path = Path(path.parts[0]) + for part in path.parts[1:]: + curr_path = curr_path / part + curr_path.mkdir(exist_ok=True) + +if error: + sys.exit(1)