-
-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcat.py
More file actions
executable file
·53 lines (38 loc) · 1.38 KB
/
cat.py
File metadata and controls
executable file
·53 lines (38 loc) · 1.38 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
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python3
import sys
import argparse
def print_file(file_path, options, counter):
try:
with open(file_path, "r", encoding="utf-8") as f:
for line in f:
stripped = line.rstrip("\n")
should_number = options.number_mode == "all" or (
options.number_mode == "non-empty" and stripped != ""
)
if should_number:
print(f"{counter}\t{stripped}")
counter += 1
else:
print(stripped)
except FileNotFoundError:
print(f"cat: {file_path}: No such file or directory", file=sys.stderr)
return counter
def main():
parser = argparse.ArgumentParser(description="Concatenate files and print output")
parser.add_argument("-n", "--number", action="store_true", help="number all lines")
parser.add_argument(
"-b", "--number-nonblank", action="store_true", help="number non-empty lines"
)
parser.add_argument("files", nargs="+", help="files to read")
args = parser.parse_args()
if args.number_nonblank:
args.number_mode = "non-empty"
elif args.number:
args.number_mode = "all"
else:
args.number_mode = "none"
counter = 1
for file in args.files:
counter = print_file(file, args, counter)
if __name__ == "__main__":
main()