Writing to Files8:00 with Kenneth Love
You often need to write to a file to store data for later use. Python makes this really straightforward!
open(filename, mode="r")opens a file. More info in the docs.
file.write("hello world") would write
"hello world" to whatever file the
file variable points at.
file.close() closes the pointer to the
The two most common modes or flags for writing are
"w" for truncating and then writing, and
"a" for appending to the file.
The context manager pattern for dealing with files is:
with open("my_file.txt", "a") as file: file.write("Hello world")
You need to sign up for Treehouse in order to download course files.Sign up