forked from CodeYourFuture/Module-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathls.py
More file actions
37 lines (28 loc) · 1.23 KB
/
ls.py
File metadata and controls
37 lines (28 loc) · 1.23 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
import argparse #for parsing command-line arguments and options
import os #to interact with the os for listing files and handling paths
import sys #Allows interaction with the Py runtime env like reading/writing output,exiting.
def list_directory(directory, show_all, one_per_line):
try:
files = os.listdir(directory)
except Exception as e:
print(f"ls: cannot access '{directory}': {e}", file=sys.stderr)
return 1
if not show_all:
files = [f for f in files if not f.startswith('.')]
files.sort()
if one_per_line:
for file in files:
print(file)
else:
print(' '.join(files))
return 0
def main():
parser = argparse.ArgumentParser(description="A simplified ls implementation.")
parser.add_argument('-1', dest='one_per_line', action='store_true', help='list one file per line')
parser.add_argument('-a', action='store_true', help='include hidden files')
parser.add_argument('directory', nargs='?', default='.', help='directory to list (default: current directory)')
args = parser.parse_args()
exit_code = list_directory(args.directory, args.a, args.one_per_line)
sys.exit(exit_code)
if __name__ == "__main__":
main()