-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathmy-ls.py
More file actions
65 lines (47 loc) · 1.29 KB
/
Copy pathmy-ls.py
File metadata and controls
65 lines (47 loc) · 1.29 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
#!/usr/bin/env python3
import sys
import os
def main():
args = sys.argv[1:]
show_all = False
one_per_line = False
paths = []
for a in args:
if a == "-a":
show_all = True
elif a == "-1":
one_per_line = True
else:
paths.append(a)
if not paths:
paths = ["."]
had_error = False
for path in paths:
if list_dir(
path,
show_all=show_all,
one_per_line=one_per_line,
):
had_error = True
if had_error:
sys.exit(1)
def list_dir(path, show_all=False, one_per_line=False):
try:
entries = os.listdir(path)
except FileNotFoundError:
print(f"ls: cannot access '{path}': No such file or directory",
file=sys.stderr,
)
return True
entries = sorted(entries)
if show_all:
normal = sorted([e for e in entries if not e.startswith('.')])
hidden = sorted([e for e in entries if e.startswith('.')])
entries = [".", ".."] + normal + hidden
else:
entries = sorted([e for e in entries if not e.startswith('.')])
for entry in entries:
print(entry)
return False
if __name__ == "__main__":
main()