-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcat.py
More file actions
27 lines (20 loc) · 761 Bytes
/
cat.py
File metadata and controls
27 lines (20 loc) · 761 Bytes
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
import argparse
import glob
parser = argparse.ArgumentParser(description="cat command")
parser.add_argument("files", nargs="+", help="Files to read")
parser.add_argument("-n", action="store_true", help="Number all lines")
parser.add_argument("-b", action="store_true", help="Number non-empty lines")
args = parser.parse_args()
files = []
for pattern in args.files:
files.extend(glob.glob(pattern))
line_number = 1
for file in files:
with open(file, "r") as f:
for line in f:
line_content = line.rstrip("\n")
if (args.b and line_content != "") or args.n:
print(f"{line_number}\t{line_content}")
line_number += 1
else:
print(line_content)