Lab 1 of 5
Β·
Guided
Handle a Missing File Gracefully
Objective: Use try/except to catch a FileNotFoundError and print a helpful message.
Instructions
Create a file called lab1.py. Write a script that tries to open a file called missing_devices.txt. Since this file does not exist, the open will fail β catch the exception and print a helpful error message instead of crashing.
Your output must match exactly:
Error: device list file not found
Please provide a valid file path
Run it with python3 lab1.py. Paste your output below and submit.
Starter Code
try:
with open("missing_devices.txt") as f:
content = f.read()
print(content)
except FileNotFoundError:
print("Error: device list file not found")
print("Please provide a valid file path")
π‘ Show Hint
The file missing_devices.txt does not exist β that is the point. Python raises FileNotFoundError when open() cannot find the file. Catch it in the except block.
Paste Your Output