-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathls.py
More file actions
42 lines (37 loc) · 930 Bytes
/
ls.py
File metadata and controls
42 lines (37 loc) · 930 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
35
36
37
38
39
40
41
42
import argparse
import os
parser = argparse.ArgumentParser(
prog="ls shell tool",
description="making ls tool with python"
)
parser.add_argument(
"-1",
dest="one_per_line",
action="store_true",
help="lists one per line"
)
parser.add_argument(
"-a",
dest="show_hidden",
action="store_true",
help="show files even hidden files"
)
parser.add_argument(
"items",
nargs="*", #defualt to current directory
help="items to list"
)
args = parser.parse_args()
paths = args.items if args.items else ['.']
for path in paths:
try:
entries = os.listdir(path)
if not args.show_hidden:
entries = [e for e in entries if not e.startswith(".")]
if args.one_per_line:
for entry in entries:
print(entry)
else:
print(" ".join(entries))
except:
print(f"ls can not access this directory: {path}")