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
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.
You need to sign up for Treehouse in order to download course files.
Sign up