forked from CodeYourFuture/Module-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlsFile.py
More file actions
34 lines (24 loc) · 758 Bytes
/
Copy pathlsFile.py
File metadata and controls
34 lines (24 loc) · 758 Bytes
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
import sys
import os
def main():
argv = sys.argv[1:]
dash = [arg for arg in argv if arg.startswith('-')]
paths = [arg for arg in argv if not arg.startswith('-')]
show_all = '-a' in dash
one_per_line = '-1' in dash
target_dir = paths[0] if paths else '.'
try:
entries = os.listdir(target_dir)
except FileNotFoundError:
print(f"ls: {target_dir}: No such file or directory", file=sys.stderr)
sys.exit(1)
if show_all:
result = ['.', '..'] + entries
else:
result = [e for e in entries if not e.startswith('.')]
if one_per_line:
print('\n'.join(result))
else:
print(' '.join(result))
if __name__ == "__main__":
main()