more-python/exercises/ex12/rm

28 lines
595 B
Plaintext
Raw Normal View History

2023-03-07 12:23:28 -06:00
#!/usr/bin/env python
2023-03-07 12:35:36 -06:00
# Exercise 12b
# rm command
2023-03-07 12:23:28 -06:00
2023-03-07 12:35:36 -06:00
import sys
import shutil
2023-03-07 12:23:28 -06:00
import argparse
from pathlib import Path
parser = argparse.ArgumentParser()
2023-03-07 12:35:36 -06:00
parser.add_argument("paths", nargs="+", type=Path)
2023-03-07 12:23:28 -06:00
parser.add_argument("-r", "--recursive", action="store_true")
args = parser.parse_args()
result = ""
2023-03-07 12:35:36 -06:00
error = False
for path in args.paths:
if path.is_dir() and args.recursive is False:
print(f"rm: no se puede borrar '{path}': Es un directorio")
error = True
elif path.is_file():
path.unlink()
else:
shutil.rmtree(path)
if error:
sys.exit(1)