Return to Dashboard

Module 24: Writing to Files

Select the best answer for each question. Score 6 out of 10 to pass.

1. Which mode should you use with `open()` to write to a file, completely overwriting its previous content if it exists?

2. If you want to add new lines to the end of an existing file without deleting its content, which mode should you use?

3. What is the primary method used on a file object (e.g., `f`) to write a string into the file?

4. What is the main advantage of using the `with open(...) as f:` structure when working with files?

5. How do you typically represent a newline character when writing to a text file in Python?

6. What happens if you use `open('my_new_file.txt', 'w')` and the file `my_new_file.txt` does not exist?

7. Consider this code: `with open('data.txt', 'a') as f: f.write("More data")`. If `data.txt` did not exist before running this, what will be the result?

8. The `f.write()` method expects its argument to be what data type?

9. If you have a list of strings, like `lines = ["First line", "Second line"]`, how would you typically write each string on a new line in a file using `f.write()`?

10. Forgetting to close a file after writing (when not using `with`) can potentially lead to...?

Back to Dashboard