generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathls.py
More file actions
executable file
·47 lines (35 loc) · 1.11 KB
/
ls.py
File metadata and controls
executable file
·47 lines (35 loc) · 1.11 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
#!/usr/bin/env python3
import sys
import os
def list_directory(path, show_all, one_per_line):
try:
entries = os.listdir(path)
if show_all:
# Add . and .. when -a is used
entries = [".", ".."] + entries
else:
entries = [e for e in entries if not e.startswith(".")]
entries.sort(key=lambda x: (x not in (".", ".."), x))
for e in entries:
print(e)
except Exception as e:
print(f"Error reading directory {path}: {e}", file=sys.stderr)
sys.exit(1)
def main():
args = sys.argv[1:]
show_all = "-a" in args
one_per_line = "-1" in args
dirs = [a for a in args if a not in ("-a", "-1")]
if not dirs:
dirs = [os.getcwd()]
for i, path in enumerate(dirs):
if os.path.isdir(path):
if len(dirs) > 1:
print(f"{path}:")
list_directory(path, show_all, one_per_line)
if len(dirs) > 1 and i < len(dirs) - 1:
print("")
else:
print(path)
if __name__ == "__main__":
main()