Recall & Review
beginner
What does the 'a' mode do when opening a file in Python?
The 'a' mode opens a file for appending. It allows you to add new data to the end of the file without deleting the existing content.
Click to reveal answer
beginner
How do you open a file named 'log.txt' to append text in Python?
Use
open('log.txt', 'a') to open the file in append mode.Click to reveal answer
beginner
Why is it important to close a file after appending data?
Closing a file ensures that all data is properly saved and resources are freed. It prevents data loss and file corruption.
Click to reveal answer
beginner
What happens if the file does not exist when you open it in append mode?
Python will create a new file if it does not exist when opened in append mode.
Click to reveal answer
beginner
Show a simple Python code snippet to append the line 'Hello, world!' to a file named 'greetings.txt'.
with open('greetings.txt', 'a') as file:
file.write('Hello, world!\n')
Click to reveal answer
What mode should you use to add data to the end of an existing file without deleting its content?
✗ Incorrect
The 'a' mode opens the file for appending, so new data is added at the end.
What happens if you open a file in 'a' mode and the file does not exist?
✗ Incorrect
Opening a file in append mode creates the file if it does not exist.
Which of the following is the correct way to append a new line to a file in Python?
✗ Incorrect
Using 'a' mode allows writing at the end without erasing existing content.
Why should you use a 'with' statement when appending to a file?
✗ Incorrect
The 'with' statement ensures the file is closed properly after the block finishes.
If you want to add multiple lines to a file, which method is best to use?
✗ Incorrect
You can combine all lines into one string with newline characters and write once for efficiency.
Explain how to append text to a file in Python and why it is useful.
Think about how to add new information without losing old data.
You got /4 concepts.
Describe what happens when you open a file in append mode and the file does not exist.
Consider how Python handles missing files in append mode.
You got /3 concepts.