Recall & Review
beginner
What does the file mode 'r' mean in Python?
The mode 'r' opens a file for reading only. The file must exist, or Python will raise an error.
Click to reveal answer
beginner
What happens if you open a file with mode 'w'?
The mode 'w' opens a file for writing. If the file exists, it will be erased (emptied). If it doesn't exist, a new file is created.
Click to reveal answer
intermediate
Explain the difference between 'a' and 'w' file modes.
Mode 'a' opens a file for appending, which means new data is added at the end without deleting existing content. Mode 'w' erases the file content before writing.
Click to reveal answer
beginner
What does the 'b' in file modes like 'rb' or 'wb' stand for?
The 'b' stands for binary mode. It means the file is opened in binary format, which is used for non-text files like images or videos.
Click to reveal answer
intermediate
What is the purpose of the '+' symbol in file modes like 'r+' or 'w+'?
The '+' symbol means the file is opened for both reading and writing.
Click to reveal answer
Which file mode opens a file for reading and writing without truncating it?
✗ Incorrect
Mode 'r+' opens the file for both reading and writing without deleting its content.
What happens if you open a non-existing file with mode 'r'?
✗ Incorrect
Mode 'r' requires the file to exist; otherwise, Python raises a FileNotFoundError.
Which mode should you use to add data to the end of an existing file without deleting its content?
✗ Incorrect
Mode 'a' opens the file for appending, adding data at the end.
What does the 'b' in 'rb' mode indicate?
✗ Incorrect
'b' means binary mode, used for non-text files.
If you want to read and write a file and also create it if it doesn't exist, which mode is best?
✗ Incorrect
Mode 'a+' opens the file for reading and writing and creates it if it doesn't exist, without truncating the file.
Describe the main file modes in Python and when to use each.
Think about whether you want to read, write, add, or work with binary files.
You got /5 concepts.
Explain what happens when you open a file in 'w' mode versus 'a' mode.
Consider if the existing content stays or is removed.
You got /3 concepts.