Track 8 · Lesson 2 of 11
The with statement and writing files
Let with close files for you, create and overwrite files with mode w, add to them with mode a, and control newlines.
You will be able to
- Use a with block so a file is always closed, even if something goes wrong
- Create or overwrite a file with mode "w" and add to the end of one with mode "a"
- Write lines correctly with write(), remembering that it does not add newlines
- Explain how Python translates newline characters when reading and writing text
Included with a Formula Quest membership
It has 4 graded exercises with feedback, hints and solutions. Python Quest is included in Formula Quest memberships that cover Python; membership and billing are managed on Formula Quest.
View Formula Quest memberships
Tracks 1 and 2, the first lesson of every track, the playground and the dataset library are free.