Skip to content

Commit 3383e76

Browse files
committed
My Cat in Python
1 parent 407b010 commit 3383e76

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import argparse
2+
parser = argparse.ArgumentParser(
3+
prog="count-containing-words",
4+
description="Counts words in a file that contain a particular character",
5+
)
6+
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")
11+
12+
args = parser.parse_args()
13+
14+
for file_path in args.path:
15+
with open(file_path, "r") as f:
16+
content = f.read()
17+
arrText = content.split("\n")
18+
19+
if args.b:
20+
for i in range(len(arrText )):
21+
if arrText[i].strip() != "":
22+
print(counterNumber,arrText[i])
23+
counterNumber += 1
24+
25+
elif args.n:
26+
for i in range(len(arrText )):
27+
print(counterNumber, arrText[i])
28+
counterNumber += 1
29+
else:
30+
print(content)

0 commit comments

Comments
 (0)