Bird
0
0

Find the error in this code that tries to read a file:

medium📝 Debug Q14 of 15
Python - File Handling Fundamentals
Find the error in this code that tries to read a file:
f = open('info.txt', 'r')
print(f.read())
f.close()
AFile 'info.txt' might not exist causing error
BFile mode should be 'w' instead of 'r'
CMissing parentheses in print statement
DFile should be opened with 'a' mode
Step-by-Step Solution
Solution:
  1. Step 1: Check file opening mode

    The code opens file in 'r' mode which is correct for reading.
  2. Step 2: Consider file existence

    If 'info.txt' does not exist, opening in 'r' mode causes a FileNotFoundError.
  3. Final Answer:

    File 'info.txt' might not exist causing error -> Option A
  4. Quick Check:

    Reading missing file = error [OK]
Quick Trick: Reading non-existent file causes error [OK]
Common Mistakes:
  • Changing mode to 'w' which overwrites file
  • Thinking print needs no parentheses in Python 3
  • Using 'a' mode which is for appending, not reading

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes