Ejercicio 12d

This commit is contained in:
perro tuerto 2023-03-13 11:46:08 -07:00
parent a016ba6c2a
commit bf58092040
1 changed files with 39 additions and 0 deletions

39
exercises/ex12/mkdir Executable file
View File

@ -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)