-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathscript-ls.py
More file actions
79 lines (62 loc) · 1.65 KB
/
script-ls.py
File metadata and controls
79 lines (62 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
import os
import sys
import argparse
def list_directory(path, show_all, one_per_line):
try:
# If path is a file, just print it
if os.path.isfile(path):
print(path)
return
# If path is a directory, list its contents
if os.path.isdir(path):
entries = os.listdir(path)
else:
print(
f"ls: cannot access '{path}': No such file or directory",
file=sys.stderr,
)
return
except PermissionError:
print(
f"ls: cannot open directory '{path}': Permission denied",
file=sys.stderr,
)
return
# Hide dotfiles unless -a is used
if not show_all:
entries = [e for e in entries if not e.startswith(".")]
entries.sort()
# Output formatting
if one_per_line:
for entry in entries:
print(entry)
else:
print(" ".join(entries))
def main():
parser = argparse.ArgumentParser(description="Simple ls implementation")
parser.add_argument(
"-a",
action="store_true",
help="include directory entries whose names begin with a dot",
)
parser.add_argument(
"-1",
dest="one_per_line",
action="store_true",
help="list one file per line",
)
parser.add_argument(
"path",
nargs="?",
default=".",
help="directory or file to list",
)
args = parser.parse_args()
list_directory(
args.path,
show_all=args.a,
one_per_line=args.one_per_line,
)
if __name__ == "__main__":
main()