Skip to content

Commit ae59977

Browse files
committed
Update files: cat/ls/wc
1 parent 37dc584 commit ae59977

File tree

3 files changed

+88
-93
lines changed

3 files changed

+88
-93
lines changed

implement-shell-tools/cat/cat.py

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,44 @@
11
import argparse
2-
parser = argparse.ArgumentParser(
3-
prog="my-cat",
4-
description="Simple cat clone with -n and -b options",
5-
)
62

7-
counterNumber = 1
8-
parser.add_argument("-n", action="store_true", help="number all lines")
9-
parser.add_argument("-b", action="store_true", help="The character to search for" )
10-
parser.add_argument("path", nargs="+", help="The file to search")
113

12-
args = parser.parse_args()
4+
def run(args):
5+
for file_path in args.path:
6+
counter_number = 1
137

14-
for file_path in args.path:
15-
with open(file_path, "r") as f:
16-
content = f.read()
17-
arrText = content.split("\n")
8+
with open(file_path, "r") as f:
9+
lines = f.readlines()
1810

1911
if args.b:
20-
for i in range(len(arrText )):
21-
if arrText[i].strip() != "":
22-
print(counterNumber,arrText[i])
23-
counterNumber += 1
12+
for line in lines:
13+
if line.strip() != "":
14+
print(counter_number, line, end="")
15+
counter_number += 1
16+
else:
17+
print(line, end="")
2418

2519
elif args.n:
26-
for i in range(len(arrText )):
27-
print(counterNumber, arrText[i])
28-
counterNumber += 1
20+
for line in lines:
21+
print(counter_number, line, end="")
22+
counter_number += 1
23+
2924
else:
30-
print(content)
25+
for line in lines:
26+
print(line, end="")
27+
28+
29+
def main():
30+
parser = argparse.ArgumentParser(
31+
prog="my-cat",
32+
description="Simple cat clone with -n and -b options",
33+
)
34+
35+
parser.add_argument("-n", action="store_true", help="number all lines")
36+
parser.add_argument("-b", action="store_true", help="The character to search for")
37+
parser.add_argument("path", nargs="+", help="The file to search")
38+
39+
args = parser.parse_args()
40+
run(args)
41+
42+
43+
if __name__ == "__main__":
44+
main()

implement-shell-tools/ls/ls.py

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import argparse
22
import os
33

4-
def show_unhidden_files(listDir):
5-
for file in listDir:
6-
if not file.startswith('.'):
4+
5+
def show_files(files, show_hidden):
6+
for file in files:
7+
if show_hidden:
78
print(file)
8-
def show_all_files(listDir):
9-
for file in listDir:
10-
print(file)
9+
else:
10+
if not file.startswith("."):
11+
print(file)
1112

1213

1314
parser = argparse.ArgumentParser(
@@ -16,24 +17,15 @@ def show_all_files(listDir):
1617
)
1718

1819
parser.add_argument("-a", action="store_true", help="include hidden files")
19-
parser.add_argument("-1", dest="one" ,action="store_true", help="use a long listing format")
20+
parser.add_argument(
21+
"-1", dest="one", action="store_true", help="list one entry per line"
22+
)
2023
parser.add_argument("path", nargs="?", default=".", help="The directory to list")
2124
args = parser.parse_args()
2225

2326

24-
25-
26-
2727
fn = args.path
2828
listDir = os.listdir(fn)
29-
if fn!="":
30-
if args.a and args.one:
31-
show_all_files(listDir)
32-
elif args.a:
33-
show_all_files(listDir)
34-
elif args.one:
35-
show_unhidden_files(listDir)
36-
37-
else:
38-
show_unhidden_files(listDir)
3929

30+
if fn != "":
31+
show_files(listDir, args.a)

implement-shell-tools/wc/wc.py

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,55 @@
11
import argparse
2+
23
parser = argparse.ArgumentParser(
34
prog="my-wc",
45
description="Simple wc clone with -l and -w options",
56
)
7+
8+
9+
10+
11+
def read_file(file_path):
12+
with open(file_path, "r") as file:
13+
return file.read()
14+
15+
16+
def count_text(text):
17+
line_count = text.count("\n")
18+
word_count = len(text.split())
19+
char_count = len(text)
20+
return line_count, word_count, char_count
21+
22+
23+
24+
625
lineCounter = 0
726
wordCounter = 0
827
charCounter = 0
28+
929
parser.add_argument("-l", action="store_true", help="count lines")
1030
parser.add_argument("-w", action="store_true", help="count words")
1131
parser.add_argument("-c", action="store_true", help="count characters")
1232
parser.add_argument("path", nargs="+", default=".", help="The file to count")
1333
args = parser.parse_args()
1434

15-
if not args.l and not args.w and not args.c:
16-
for file_path in args.path:
17-
with open(file_path, "r") as f:
18-
content = f.read()
19-
arrText = content.split("\n")
20-
lineCounter += len(arrText)
21-
arrWords = content.split()
22-
wordCounter += len(arrWords)
23-
charCounter += len(content)
24-
print("Line count:", lineCounter,"lines")
25-
print("Word count:", wordCounter,"words")
26-
print("Character count:", charCounter,"characters")
27-
elif args.l and args.w:
28-
for file_path in args.path:
29-
with open(file_path, "r") as f:
30-
content = f.read()
31-
arrText = content.split("\n")
32-
lineCounter += len(arrText)
33-
arrWords = content.split()
34-
wordCounter += len(arrWords)
35-
print("Line count:", lineCounter,"lines")
36-
print("Word count:", wordCounter,"words")
37-
elif args.l and args.c:
38-
for file_path in args.path:
39-
with open(file_path, "r") as f:
40-
content = f.read()
41-
arrText = content.split("\n")
42-
lineCounter += len(arrText)
43-
charCounter += len(content)
44-
print("Line count:", lineCounter,"lines")
45-
print("Character count:", charCounter,"characters")
46-
elif args.l:
47-
for file_path in args.path:
48-
with open(file_path, "r") as f:
49-
content = f.read()
50-
arrText = content.split("\n")
51-
lineCounter += len(arrText)
52-
print("Line count:", lineCounter,"lines")
53-
54-
elif args.w:
55-
for file_path in args.path:
56-
with open(file_path, "r") as f:
57-
content = f.read()
58-
arrText = content.split()
59-
wordCounter += len(arrText)
60-
print("Word count:", wordCounter,"words")
61-
elif args.c:
62-
for file_path in args.path:
63-
with open(file_path, "r") as f:
64-
content = f.read()
65-
charCounter += len(content)
66-
print("Character count:", charCounter,"characters")
35+
36+
37+
for path in args.path:
38+
text = read_file(path)
39+
lines, words, chars = count_text(text)
40+
41+
lineCounter += lines
42+
wordCounter += words
43+
charCounter += chars
44+
45+
if args.l:
46+
print(lines, path)
47+
elif args.w:
48+
print(words, path)
49+
elif args.c:
50+
print(chars, path)
51+
else:
52+
print(lines, words, chars, path)
53+
54+
if len(args.path) > 1 and not args.l and not args.w and not args.c:
55+
print(lineCounter, wordCounter, charCounter, "total")

0 commit comments

Comments
 (0)