Enrol to start learning
Reading is open to everyone. Enrolling is free, and it is what unlocks the audio lessons, practice tests and progress tracking.
13.5. File Handling in Python
This section
Practice test
11 questions on this section. Wrong answers show you what to read again.
Sign up to take itWhole chapter
Revision test
Mixed questions from across the chapter. Your answers get marked.
Sign up to take itQuick
Flashcard drill
3 cards from this lesson. Good the night before a test.
Try these first
- 1.
What function do you use to open a file in Python?
Hint
Think of the command used to access files in Python.
- 2.
What mode do you use to open a file for writing?
Hint
Remember that w stands for a specific action.
- 3.
What is the mode used to open a file for writing in Python?
- r
- w
- a
- rb
Hint
It’s the first letter of 'write'.
- 4.
Context managers are used to automatically handle which of the following?
- True
- False
Hint
Think about what happens to resources when you're done using them.
- 5.
Create a Python function that takes a file name and writes multiple lines of text to it. Ensure it handles cases where the file already exists.
Hint
Consider how to manage existing files and whether to overwrite them.
- 6.
Write a piece of code that reads a CSV file and prints each row. Explain how you would handle exceptions if the file is not found.
Hint
Think about the mechanism to alert the user when the file doesn’t exist.
Exercises
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting
3 more questions available
Enrol freeQuiz
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting
2 more questions available
Enrol freeChallenge Problems
Total Questions
2
Estimated Time
4 min
Passing Score
70%
Instructions
- Read each question carefully
- You can use hints if you need help
- Complete all questions before submitting