Skip to content

Commit b6b1e02

Browse files
committed
implementd ls,wc, and cat cli in python
1 parent 4350f48 commit b6b1e02

10 files changed

Lines changed: 186 additions & 0 deletions

File tree

‎.DS_Store‎

10 KB
Binary file not shown.
6 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world, Hi, ETC
6 KB
Binary file not shown.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog="cat",
5+
description="Concatenate and display files",
6+
)
7+
8+
parser.add_argument(
9+
"-n",
10+
action="store_true",
11+
help="number all lines",
12+
)
13+
14+
parser.add_argument(
15+
"-b",
16+
action="store_true",
17+
help="number non-empty lines",
18+
)
19+
20+
parser.add_argument(
21+
"files",
22+
nargs="+",
23+
help="the files to read",
24+
)
25+
26+
args = parser.parse_args()
27+
28+
line_number = 1
29+
30+
for filename in args.files:
31+
try:
32+
with open(filename) as file:
33+
for line in file:
34+
content = line.rstrip("\n")
35+
36+
if args.b and content == "":
37+
print()
38+
continue
39+
40+
if args.n or args.b:
41+
print(f"{line_number}\t{content}")
42+
line_number +=1
43+
else:
44+
print(content)
45+
46+
except OSError as error:
47+
print(f"{error.strerror}")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import argparse
2+
import os
3+
4+
parser = argparse.ArgumentParser(
5+
prog = "ls",
6+
description = "List directory content"
7+
)
8+
9+
parser.add_argument(
10+
"paths",
11+
nargs = "*",
12+
default=["."],
13+
help= "the directory path to process",
14+
)
15+
16+
parser.add_argument(
17+
"-1",
18+
"--one",
19+
action="store_true",
20+
help="list one file per line",
21+
22+
)
23+
24+
parser.add_argument(
25+
"-a",
26+
"--all",
27+
action="store_true",
28+
help="List all files including hidden files such as '.' and '..' "
29+
)
30+
31+
args = parser.parse_args()
32+
33+
for target_dir in args.paths:
34+
try:
35+
files = os.listdir(target_dir)
36+
37+
if args.all:
38+
file_list = sorted([".", ".."] + files)
39+
else:
40+
file_list = []
41+
for name in files:
42+
if not name.startswith("."):
43+
file_list.append(name)
44+
file_list.sort()
45+
46+
if args.one:
47+
for name in file_list:
48+
print(name)
49+
else:
50+
print(" ".join(file_list))
51+
except OSError as error:
52+
print(f"{error.strerror}")
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import argparse
2+
3+
parser = argparse.ArgumentParser(
4+
prog ="wc",
5+
description ="my word, line and byte count",
6+
)
7+
8+
parser.add_argument(
9+
"-l",
10+
"--lines",
11+
action="store_true",
12+
help="count lines"
13+
)
14+
15+
parser.add_argument(
16+
"-w",
17+
"--words",
18+
action="store_true",
19+
help="count words",
20+
)
21+
22+
parser.add_argument(
23+
"-c",
24+
"--bytes",
25+
action ="store_true",
26+
help="count bytes",
27+
)
28+
29+
parser.add_argument(
30+
"files",
31+
nargs="+",
32+
help=" the files to process",
33+
)
34+
35+
args = parser.parse_args()
36+
37+
if not args.lines and not args.words and not args.bytes:
38+
show_line = True
39+
show_words = True
40+
show_bytes = True
41+
else:
42+
show_line = args.lines
43+
show_words = args.words
44+
show_bytes = args.bytes
45+
46+
total_lines = 0
47+
total_words = 0
48+
total_bytes = 0
49+
50+
51+
for filename in args.files:
52+
try:
53+
with open(filename, "rb") as f:
54+
content = f.read()
55+
56+
lines = content.count(b"\n")
57+
words = len(content.split())
58+
bytes_count = len(content)
59+
60+
total_lines += lines
61+
total_words += words
62+
total_bytes += bytes_count
63+
64+
output = []
65+
if show_line:
66+
output.append(str(lines))
67+
if show_words:
68+
output.append(str(words))
69+
if show_bytes:
70+
output.append(str(bytes_count))
71+
output.append(filename)
72+
print("\t".join(output))
73+
74+
except OSError as error:
75+
print(f"{error.strerror}")
76+
77+
if len(args.files) > 1:
78+
total_output = []
79+
if show_line:
80+
total_output.append(str(total_lines))
81+
if show_words:
82+
total_output.append(str(total_words))
83+
if show_bytes:
84+
total_output.append(str(total_bytes))
85+
total_output.append("total")
86+
print("\t".join(total_output))

‎implement-shell-tools/.DS_Store‎

6 KB
Binary file not shown.
6 KB
Binary file not shown.

‎individual-shell-tools/.DS_Store‎

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)