Skip to content

Commit 575b3f9

Browse files
Improve error handling and formatting in cat.py
1 parent d72fc0a commit 575b3f9

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import sys
23
from enum import Enum
34

45
class Numbering(Enum):
@@ -25,11 +26,16 @@ def cat(filepath, numbering, start_line):
2526
else:
2627
print(line, end='')
2728
except FileNotFoundError:
28-
print(f"cat: {filepath}: No such file or directory")
29+
print(
30+
f"cat: {filepath}: No such file or directory",
31+
file=sys.stderr
32+
)
2933
return line_number
3034

3135
def main():
32-
parser = argparse.ArgumentParser(description="Concatenate files and print on the standard output.")
36+
parser = argparse.ArgumentParser(
37+
description="Concatenate files and print on the standard output."
38+
)
3339
parser.add_argument('-n', action='store_true', help='number all output lines')
3440
parser.add_argument('-b', action='store_true', help='number non-empty output lines')
3541
parser.add_argument('files', nargs='+', help='files to concatenate')
@@ -44,7 +50,8 @@ def main():
4450
else:
4551
numbering = Numbering.NONE
4652

47-
line_number = 1 # start line numbering
53+
line_number = 1
54+
4855
for file in args.files:
4956
line_number = cat(file, numbering=numbering, start_line=line_number)
5057

0 commit comments

Comments
 (0)