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
If you write to a file, there's a really good chance you're going to want to read from that file. Here's how to read in the contents of a file in Python.
open(filename, mode="r")
opens a file. More info in the docs.
file.read(bytes=-1)
would read the entire contents of the file. You can control the number of bytes read by passing in an integer. Relatedly, file.seek()
will move the read/write pointer to another part of the file.
file.readlines()
reads the entire file into a list, with each line as a list item.
The context manager pattern for dealing with files is:
with open("my_file.txt", "r") as file:
file.read(10)
For more about sys.argv
, check out the docs.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up-
Christopher Stuart
Full Stack JavaScript Techdegree Graduate 27,771 PointsWhy is there no workspace avail for these videos?
1 Answer
-
fahad lashari
7,693 PointsHow do I delete something specific from a file? Python I/O
Posted by fahad lasharifahad lashari
7,693 Points1 Answer
-
PLUS
Piyush Chawla
Courses Plus Student 30 Points1 Answer
-
Elise Gordillo
1,601 Points2 Answers
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