Skip to content

Commit aad36ba

Browse files
committed
cat folder code upadate for -n flag
1 parent c4f47a0 commit aad36ba

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import argparse
22
import os
33

4+
# set argument parser
45
parser = argparse.ArgumentParser(description="Python implementation of cat command")
56

7+
# to number all lines
8+
parser.add_argument("-n", action="store_true", help="Number all lines")
9+
# To raed files
610
parser.add_argument("files", nargs="+", help="Files to read")
711

812
args = parser.parse_args()
@@ -17,4 +21,8 @@
1721

1822
with open(file, "r") as f:
1923
for line in f:
20-
print(line, end ="")
24+
if args.n:
25+
print(f"{line_number:6}\t{line}", end="") # to print number all lines
26+
line_number += 1
27+
else:
28+
print(line, end ="") # to print content of files

0 commit comments

Comments
 (0)