more-python/exercises/ex06/find.py

46 lines
1.1 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# Exercise 6
# 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)