LearnFrenzy
Sign in Take a free test

File Handling · Predict the Output · Question 8

Catching FileNotFoundError Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 8 Back to Exercise

What is the output of the following Python program?

main.py
import os, tempfile
os.chdir(tempfile.mkdtemp())

try:
    open("missing.txt")
except FileNotFoundError as e:
    print(type(e).__name__, isinstance(e, OSError), e.filename)
  • A FileNotFoundError False missing.txt
  • B FileNotFoundError True missing.txt Correct
  • C OSError True missing.txt
  • D IOError True None
Correct Answer: Option B

FileNotFoundError is an OSError, and the exception records the file name.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links