Compare commits
3 Commits
1f84479a80
...
a016ba6c2a
Author | SHA1 | Date |
---|---|---|
perro tuerto | a016ba6c2a | |
perro tuerto | 6d37344f7c | |
perro tuerto | 675fd8ad69 |
|
@ -0,0 +1,34 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# Exercise 12a
|
||||||
|
# ls command
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("paths", default=Path().cwd(), nargs="*", type=Path)
|
||||||
|
parser.add_argument("-a", "--all", action="store_true")
|
||||||
|
args = parser.parse_args()
|
||||||
|
result = ""
|
||||||
|
|
||||||
|
|
||||||
|
def get_items(path, with_hidden):
|
||||||
|
items = path.glob("*")
|
||||||
|
if not with_hidden:
|
||||||
|
items = list(filter(lambda i: i.name[0] != ".", items))
|
||||||
|
return list(sorted(set(items)))
|
||||||
|
|
||||||
|
|
||||||
|
if isinstance(args.paths, list):
|
||||||
|
roots = list(map(lambda p: get_items(p, args.all), args.paths))
|
||||||
|
else:
|
||||||
|
roots = [get_items(args.paths, args.all)]
|
||||||
|
|
||||||
|
for items in roots:
|
||||||
|
if len(items) > 0:
|
||||||
|
root = items[0].parent
|
||||||
|
items = "\n".join(list(map(lambda i: i.name, items)))
|
||||||
|
result += items if len(roots) == 1 else f"{root}:\n{items}\n\n"
|
||||||
|
|
||||||
|
if result != "":
|
||||||
|
print(result.strip())
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
# Exercise 12b
|
||||||
|
# rm command
|
||||||
|
|
||||||
|
import sys
|
||||||
|
import shutil
|
||||||
|
import argparse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument("paths", nargs="+", type=Path)
|
||||||
|
parser.add_argument("-r", "--recursive", action="store_true")
|
||||||
|
args = parser.parse_args()
|
||||||
|
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)
|
|
@ -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)
|
Loading…
Reference in New Issue