diff --git a/exercises/ex06/ex06.py b/exercises/ex06/ex06.py new file mode 100644 index 0000000..f6aacdd --- /dev/null +++ b/exercises/ex06/ex06.py @@ -0,0 +1,44 @@ +# Exercise 2 +# 1. find command + +import sys +import argparse +from pathlib import Path + + +def list(args): + if not args["path"].exists(): + print(f"find: ‘{args['path']}’: No existe el fichero o el directorio") + sys.exit(1) + for file in sorted(args["path"].glob(get_query(args))): + if args["exec"]: + ... + else: + print(file.relative_to(".")) + + +def get_query(args): + if args["type"] is not None: + if args["type"] == "f": + return "**/*.*" + elif args["type"] == "d": + return "**" + else: + return f"**/{args['name']}" + + +parser = argparse.ArgumentParser() +parser.add_argument("-name", default="**/*", type=str) +parser.add_argument("-type", choices=["f", "d"]) +parser.add_argument("-print", default=True, action="store_true") +parser.add_argument("-exec", action="extend", nargs="+") + +if len(sys.argv) > 1: + path = Path(sys.argv[1]) + del sys.argv[1] + args = parser.parse_args().__dict__ + args["path"] = path +else: + args = {"path": Path("."), "name": "*", "type": None, "exec": None} + +list(args)