-
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathcat.py
More file actions
42 lines (31 loc) · 1.22 KB
/
cat.py
File metadata and controls
42 lines (31 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import argparse
import os
# set argument parser
parser = argparse.ArgumentParser(description="Python implementation of cat command")
# to number all lines
parser.add_argument("-n", action="store_true", help="Number all lines")
# to number non-blank lines
parser.add_argument("-b", action="store_true", help="Number non-blank lines (overrides -n)")
# To raed files
parser.add_argument("files", nargs="+", help="Files to read")
args = parser.parse_args()
if args.n and args.b:
args.n = False
line_number = 1
for file in args.files:
if not os.path.isfile(file):
print(f"cat: {file}: No such file or directory")
continue
with open(file, "r") as f:
for line in f:
if args.b:
if line.strip(): #to number non blank lines only
print(f"{line_number:6}\t{line}", end="")
line_number += 1
else:
print(line, end="") # to print blank line with number
elif args.n:
print(f"{line_number:6}\t{line}", end="") # to print number all lines
line_number += 1
else:
print(line, end ="") # to print content of files