Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll

- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
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 up-
Jiawei Hu
9,740 Points1 Answer
-
Swan The Human
Full Stack JavaScript Techdegree Student 19,338 Points1 Answer
-
Youssef Moustahib
7,779 Points1 Answer
-
PLUS
hamdi ismail
Courses Plus Student 8,927 Points1 Answer
-
dmitriy ignatiev
Python Web Development Techdegree Student 1,789 Points3 Answers
-
Daniel Petrov
3,495 Points2 Answers
-
PLUS
Piyush Chawla
Courses Plus Student 30 Points1 Answer
View all discussions for this video
Related 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