-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathls.py
More file actions
executable file
·42 lines (31 loc) · 1.04 KB
/
ls.py
File metadata and controls
executable file
·42 lines (31 loc) · 1.04 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
#!/usr/bin/env python3
import sys
import os
import argparse
def list_directory(path, show_all):
try:
entries = os.listdir(path)
except Exception as e:
print(f"Error reading directory {path}: {e}", file=sys.stderr)
sys.exit(1)
for entry in sorted(entries):
if not show_all and entry.startswith("."):
continue
print(entry)
def main():
parser = argparse.ArgumentParser(description="List directory contents")
parser.add_argument("-a", "--all", action="store_true", help="show hidden files")
parser.add_argument(
"-1", dest="one", action="store_true", help="one entry per line (default)"
)
parser.add_argument("paths", nargs="*", default=["."], help="directories to list")
args = parser.parse_args()
dirs = args.paths
for i, path in enumerate(dirs):
if len(dirs) > 1:
print(f"{path}:")
list_directory(path, args.all)
if len(dirs) > 1 and i < len(dirs) - 1:
print("")
if __name__ == "__main__":
main()