Skip to content

Commit d341d5c

Browse files
committed
Implementing shell tools: cat, ls, and wc. in Python.
1 parent 407b010 commit d341d5c

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
3+
def cat_file(filename, flag):
4+
try:
5+
with open(filename, 'r') as file:
6+
if not flag:
7+
for line in file:
8+
print(line, end="")
9+
10+
elif flag == "-n":
11+
for index, line in enumerate(file, start=1):
12+
print(f"{index:6}\t{line}", end="")
13+
14+
elif flag == "-b":
15+
line_number = 1
16+
for line in file:
17+
if line.strip() == "":
18+
print(line, end="")
19+
else:
20+
print(f"{line_number:6}\t{line}", end="")
21+
line_number += 1
22+
23+
except FileNotFoundError:
24+
print(f"cat: {filename}: No such file or directory", file=sys.stderr)
25+
26+
def main():
27+
args = sys.argv[1:]
28+
29+
flag = False
30+
31+
if "-n" in args:
32+
flag = "-n"
33+
args.remove("-n")
34+
elif "-b" in args:
35+
flag = "-b"
36+
args.remove("-b")
37+
38+
for filename in args:
39+
cat_file(filename, flag)
40+
41+
if __name__ == "__main__":
42+
main()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
import os
3+
4+
def main():
5+
args = sys.argv[1:]
6+
7+
path = "."
8+
one_per_line = False
9+
show_hidden = False
10+
11+
for arg in args:
12+
if arg == "-a":
13+
show_hidden = True
14+
elif arg == "-1":
15+
one_per_line = True
16+
elif not arg.startswith("-"):
17+
path = arg
18+
19+
entries = os.listdir(path)
20+
21+
entries.sort()
22+
23+
if not show_hidden:
24+
entries = [f for f in entries if not f.startswith(".")]
25+
26+
if one_per_line:
27+
for entry in entries:
28+
print(entry)
29+
else:
30+
print(" ".join(entries))
31+
32+
if __name__ == "__main__":
33+
main()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import sys
2+
3+
def count_file(filename):
4+
5+
with open(filename, 'r') as file:
6+
content = file.read()
7+
8+
lines = len(content.splitlines())
9+
words = len(content.split())
10+
chars = len(content)
11+
12+
return (lines, words, chars)
13+
14+
15+
def format_output(lines, words, chars, filename, show_l, show_w, show_c):
16+
parts = []
17+
18+
if show_l:
19+
parts.append(f"{lines:8}")
20+
if show_w:
21+
parts.append(f"{words:8}")
22+
if show_c:
23+
parts.append(f"{chars:8}")
24+
25+
parts.append(filename)
26+
27+
return " ".join(parts)
28+
29+
def main():
30+
args = sys.argv[1:]
31+
32+
l = w = c = False
33+
files = []
34+
35+
for arg in args:
36+
if arg == "-l":
37+
l = True
38+
elif arg == "-w":
39+
w = True
40+
elif arg == "-c":
41+
c = True
42+
elif not arg.startswith("-"):
43+
files.append(arg)
44+
45+
if not (l or w or c):
46+
l = w = c = True
47+
48+
total_lines = total_words = total_chars = 0
49+
processed_files = 0
50+
51+
for filename in files:
52+
result = count_file(filename)
53+
if result:
54+
lines, words, chars = result
55+
print(format_output(lines, words, chars, filename, l, w, c))
56+
57+
total_lines += lines
58+
total_words += words
59+
total_chars += chars
60+
processed_files += 1
61+
62+
if processed_files > 1:
63+
print(format_output(total_lines, total_words, total_chars, "total", l, w, c))
64+
65+
if __name__ == "__main__":
66+
main()

0 commit comments

Comments
 (0)