-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.py
More file actions
executable file
·38 lines (31 loc) · 1.19 KB
/
Copy pathcat.py
File metadata and controls
executable file
·38 lines (31 loc) · 1.19 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
#!/usr/bin/env python3
import argparse
import sys
parser = argparse.ArgumentParser(
prog='cat',
description='CLI tool to concatenate and print files',
)
parser.add_argument('-n', '--number', action='store_true', help='Number all outputs, starting at 1')
parser.add_argument('-b', '--nonBlank', action='store_true', help='Number only non-blank lines, starting at 1')
parser.add_argument('files', nargs='*', help='Files to read')
args = parser.parse_args()
# print(args.number, args.nonBlank, args.files)
# print(sys.argv[0], sys.argv[1])
for file in args.files:
count = 1
try:
with open(file, 'r') as f:
for line in f:
if args.nonBlank:
if line.strip():
print(f"{str(count).rjust(6)}\t{line}", end='')
count += 1
else:
print(line, end="")
elif args.number:
print(f"{str(count).rjust(6)}\t{line}", end='')
count +=1
else:
print(f"{line}", end="")
except FileNotFoundError:
print(f"cat: {file}: No such file or directory.", file=sys.stderr)