Skip to content

Commit b711630

Browse files
committed
Complete ls.py implementation.
1 parent 736017d commit b711630

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

  • implement-shell-tools/ls

‎implement-shell-tools/ls/ls.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
import os
3+
4+
show_all = "-a" in sys.argv
5+
one_per_line = "-1" in sys.argv
6+
7+
paths = []
8+
9+
for argument in sys.argv[1:]:
10+
if not argument.startswith("-"):
11+
paths.append(argument)
12+
13+
if not paths:
14+
paths.append(".")
15+
16+
for path in paths:
17+
if os.path.isfile(path):
18+
if len(paths) == 1 or one_per_line:
19+
print(path)
20+
else:
21+
print(path, end = (" "))
22+
else:
23+
if len(paths) > 1:
24+
print()
25+
print()
26+
print(path + ":")
27+
28+
files = sorted(os.listdir(path))
29+
30+
if show_all:
31+
files = ["."] + [".."] + files
32+
33+
for file in files:
34+
if show_all or not file.startswith("."):
35+
if one_per_line:
36+
print(file)
37+
else:
38+
print(file, end=(" "))
39+
if not one_per_line:
40+
print()
41+
42+

0 commit comments

Comments
 (0)