Handling Missing Files
Completed
Create exists.txt containing hello. Read file names one per line (first line n); for each print its content or missing: NAME using FileNotFoundError.
Input
Line 1: n. Then n file names.
Output
n lines.
Example
Input
2 exists.txt nope.txt
Output
hello missing: nope.txt
Editor
Python
Hints
Opening a file that doesn't exist raises FileNotFoundError.
Solution explanation
File operations fail for reasons outside your program's control, so handle the errors.
Show solution
with open("exists.txt", "w") as f:
f.write("hello")
for _ in range(int(input())):
name = input()
try:
with open(name) as f:
print(f.read())
except FileNotFoundError:
print(f"missing: {name}")