Skip to content

Commit b946931

Browse files
Boshra MahmoudiBoshra Mahmoudi
authored andcommitted
put the code in try block and catch FileNotFoundError
1 parent 72663a7 commit b946931

1 file changed

Lines changed: 17 additions & 14 deletions

File tree

  • implement-shell-tools/cat

implement-shell-tools/cat/cat.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,20 @@
1414
line_number = 1
1515

1616
for filename in args.files:
17-
18-
with open(filename) as file:
19-
for line in file:
20-
line = line.rstrip("\n")
21-
22-
if args.b and line == "":
23-
print()
24-
continue
25-
26-
if args.n or args.b:
27-
print(f"{line_number} {line}")
28-
line_number += 1
29-
else:
30-
print(line)
17+
try:
18+
with open(filename) as file:
19+
for line in file:
20+
line = line.rstrip("\n")
21+
22+
if args.b and line == "":
23+
print()
24+
continue
25+
26+
if args.n or args.b:
27+
print(f"{line_number} {line}")
28+
line_number += 1
29+
else:
30+
print(line)
31+
32+
except FileNotFoundError:
33+
print(f"{filename}: No such file or directory")

0 commit comments

Comments
 (0)