Quiz: Python file handling and exceptions
This quiz covers essential concepts of Python file handling and exception management, focusing on practical applications and common scenarios.
Quiz(24 questions)
1. What does the 'with' statement ensure when opening files in Python?
Terms in this Study Set(24)
Flashcards 1(12)
What is file handling in Python?
File handling refers to the process of creating, reading, updating, and deleting files in Python using built-in functions.
How do you open a file in Python?
Use the built-in function `open(filename, mode)` where mode can be 'r', 'w', 'a', etc.
True or False: 'r' mode overwrites a file.
False. 'r' mode opens a file for reading only; it cannot overwrite.
Fill in the blank: To close a file, use the method ______.
.close()
What is the purpose of exceptions in Python?
Exceptions allow handling errors gracefully, preventing program crashes and managing unexpected situations.
What is the 'with' statement used for?
The 'with' statement simplifies file handling by automatically closing the file after the block is executed.
Compare 'w' and 'a' modes.
'w': Write mode, overwrites existing files. 'a': Append mode, adds to existing files.
Cause → Effect: What happens when you try to open a non-existent file in 'r' mode?
An IOError is raised, indicating the file cannot be found.
How do you read contents of a file?
Use `file.read()` to read all contents or `file.readline()` for one line at a time.
What is a try-except block?
A try-except block is used to catch and handle exceptions in Python, providing a way to respond to errors.
What does the 'finally' block do?
The 'finally' block executes code regardless of whether an exception was raised, often used for cleanup.
How to write data to a file?
Open a file in 'w' or 'a' mode, then use `file.write(data)` to write the desired content.
Flashcards 2(12)
What does 'with open' do in Python?
Automatically handles opening and closing files. - Ensures files are properly closed, even if an error occurs.
True or False: Exceptions crash the program.
False - Exceptions can be caught and handled using try-except blocks.
List two built-in exceptions in Python.
1. ValueError 2. FileNotFoundError
Fill in the blank: To read a file, use ______.
file.read()
How to handle multiple exceptions?
Use a tuple in the except clause: try: ... except (TypeError, ValueError): ...
What is the purpose of 'finally'?
'finally' block always executes after try-except, regardless of exceptions, for cleanup actions.
Compare 'read()' vs 'readline()'.
'read()' reads the entire file; 'readline()' reads one line at a time.
Cause → Effect: FileNotFoundError occurs when...
...you try to open a file that does not exist.
Example: Write to a file in Python.
with open('output.txt', 'w') as file: file.write('Hello, World!')
What is a context manager?
A construct that simplifies resource management, ensuring resources are properly managed.
True or False: You must manually close a file opened with 'with'.
False - 'with' automatically closes the file.
Describe the role of 'try' in error handling.
'try' block allows you to test a block of code for errors, catching exceptions.
Questions in this Study Set(24)
1. What does the 'with' statement ensure when opening files in Python?
2. What does the 'a' mode do when opening a file in Python?
3. True or False: The try block can contain multiple statements.
4. Which statement correctly describes the purpose of exceptions in Python?
5. Which of the following is NOT a built-in exception in Python?
6. What happens if you try to read from a closed file?
7. What does the 'write()' method do?
8. Which of the following is NOT a valid mode for opening a file in Python?
9. How can you handle multiple specific exceptions in one line?
10. When using the 'with' statement for file handling, what is its main advantage?
11. What happens if you do not catch an exception in Python?
12. How can you write multiple lines to a file in Python?
13. What is the primary role of the 'finally' block?
14. What does the 'finally' block in a try-except structure do?
15. Which method reads the entire content of a file at once?
16. How do you read a single line from a file in Python?
17. Fill in the blank: To open a file for reading, you use 'open(filename, ____)'.
18. What is the effect of opening a file in 'w' mode?
19. What does the exception FileNotFoundError indicate?
20. What kind of error is raised if you attempt to open a non-existent file in 'r' mode?
21. Which of the following correctly demonstrates writing to a file safely?
22. Which method is used to close an open file in Python?
23. What is a context manager primarily used for?
24. How can you handle an exception that occurs during file operations?
Related Study Sets
Abitur Rekursion
Abitur: Abitur Klassen und Objekte
Was ist ein Algorithmus Schritt für Schritt
if und Schleifen Notizen
Wiederholung: Funktionen
Test: Binärzahlen
Listen Notizen
Schleife Alltag Beispiel Begriffe
Create Your Own Study Set
Upload a PDF, paste your notes, or describe a topic – AI generates flashcards, quizzes and more in seconds.

