#!/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)