Ejercicio 12c

This commit is contained in:
perro tuerto 2023-03-07 10:57:06 -08:00
parent 6d37344f7c
commit a016ba6c2a
2 changed files with 31 additions and 1 deletions

View File

@ -11,7 +11,6 @@ parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="+", type=Path)
parser.add_argument("-r", "--recursive", action="store_true")
args = parser.parse_args()
result = ""
error = False
for path in args.paths:

31
exercises/ex12/rmdir Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python
# Exercise 12c
# rmdir 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
for path in args.paths:
if path.is_file():
print(f"rmdir: fallo al borrar '{path}': No es un directorio")
error = True
elif len(list(path.glob("*"))) > 0:
print(f"rmdir: fallo al borrar '{path}': El directorio no está vacío")
error = True
else:
if args.parents is True:
while len(path.parts) > 0:
path.rmdir()
path = path.parent
else:
path.rmdir()
if error:
sys.exit(1)