Skip to content

Commit 62ddefa

Browse files
committed
Implement ls, cat and wc in Python
1 parent d3857c2 commit 62ddefa

3 files changed

Lines changed: 134 additions & 0 deletions

File tree

implement-shell-tools/cat/cat.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import sys
2+
3+
args = sys.argv[1:]
4+
5+
show_n = "-n" in args
6+
show_b = "-b" in args
7+
8+
files = [arg for arg in args if not arg.startswith("-")]
9+
10+
content = ""
11+
12+
for file in files:
13+
with open(file, "r") as f:
14+
content += f.read()
15+
16+
lines = content.split("\n")
17+
18+
if lines and lines[-1] == "":
19+
lines.pop()
20+
21+
output = []
22+
23+
if show_n:
24+
for i, line in enumerate(lines, start=1):
25+
output.append(f"{str(i).rjust(6)} {line}")
26+
27+
elif show_b:
28+
count = 1
29+
30+
for line in lines:
31+
if line != "":
32+
output.append(f"{str(count).rjust(6)} {line}")
33+
count += 1
34+
else:
35+
output.append("")
36+
37+
else:
38+
output = lines
39+
40+
print("\n".join(output))

implement-shell-tools/ls/ls.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
import sys
3+
4+
args = sys.argv[1:]
5+
6+
show_all = "-a" in args
7+
8+
directory = "."
9+
10+
for arg in args:
11+
if not arg.startswith("-"):
12+
directory = arg
13+
14+
files = sorted(os.listdir(directory))
15+
16+
if show_all:
17+
files = [".", ".."] + files
18+
else:
19+
files = [file for file in files if not file.startswith(".")]
20+
21+
for file in files:
22+
print(file)

implement-shell-tools/wc/wc.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import sys
2+
3+
args = sys.argv[1:]
4+
5+
show_lines = "-l" in args
6+
show_words = "-w" in args
7+
show_bytes = "-c" in args
8+
9+
files = [arg for arg in args if not arg.startswith("-")]
10+
11+
def get_stats(text):
12+
return {
13+
"lines": text.count("\n"),
14+
"words": len(text.split()),
15+
"bytes": len(text.encode("utf-8"))
16+
}
17+
18+
def format_num(num):
19+
return str(num).rjust(8)
20+
21+
total_lines = 0
22+
total_words = 0
23+
total_bytes = 0
24+
25+
for file in files:
26+
with open(file, "r") as f:
27+
content = f.read()
28+
29+
stats = get_stats(content)
30+
31+
total_lines += stats["lines"]
32+
total_words += stats["words"]
33+
total_bytes += stats["bytes"]
34+
35+
output = []
36+
37+
if show_lines:
38+
output.append(format_num(stats["lines"]))
39+
elif show_words:
40+
output.append(format_num(stats["words"]))
41+
elif show_bytes:
42+
output.append(format_num(stats["bytes"]))
43+
else:
44+
output.extend([
45+
format_num(stats["lines"]),
46+
format_num(stats["words"]),
47+
format_num(stats["bytes"])
48+
])
49+
50+
output.append(file)
51+
52+
print(" ".join(output))
53+
54+
if len(files) > 1:
55+
output = []
56+
57+
if show_lines:
58+
output.append(format_num(total_lines))
59+
elif show_words:
60+
output.append(format_num(total_words))
61+
elif show_bytes:
62+
output.append(format_num(total_bytes))
63+
else:
64+
output.extend([
65+
format_num(total_lines),
66+
format_num(total_words),
67+
format_num(total_bytes)
68+
])
69+
70+
output.append("total")
71+
72+
print(" ".join(output))

0 commit comments

Comments
 (0)