Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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 file
file.
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")
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up