File input/output (i/o)
NOTE: you need to run some of the “writing” code in this notebook in order for the “reading” code to work in this notebook in interactive mode!
It is frequently the case that we want to be able to interact with files programmatically. This is where file i/o comes in.
input: The file being read/processed by the program.
output: The file that the program produces.
In python, we’ll use the open()
function to interact with files:
The parameters and return value are as follows:
filename
: a string indicating the location of the filemode
: a string indicating what we want to do with this file (the three modes we care about for this class are listed below)- “w” : write
- “r” : read
- “a” : append
file_obj
: the return value of this function is an object called a “TextIOWrapper” – we’ll refer to this as our “file object”. See notes 12.1 for more information on objects. You can also look at the python documentation for more info on interacting with file objects.
Let’s look at an example. For all of these examples, you should run them then go look at the contents of the file!
Writing to files
To write to a file in python, you need to do two things:
- Use the
open(filename, mode)
function in the write mode (“w”). - Use the
file.write(text)
function to actually write to the file. This function takes a string parameter to write to the file.
Notice two things:
file.write(text)
does not include a newline character by default.- If we open the file again in write mode, we will overwrite the previous contents.
Appending to files
To append to a file in python, you need to do two things:
- Use the
open(filename, mode)
function in the append mode (“a”). - Use the
file.write(text)
function to actually write to the file. This function takes a string parameter to write to the file.
The difference between write and append is that append will add to the end of the file rather than overwriting the contents.
Reading from files
To read from a file in python, you need to do one thing, then you have lots of options:
- Use the
open(filename, mode)
function in the append mode (“r”).
There are three ways to read a file’s contents that we will cover:
- Use the
file.read()
function. - Use the
file.readline()
function. - Use a loop. (This can be a
for
loop or awhile
loop!)
Imagine that when we are reading the file, we are advancing a cursor through its contents. Which function we use just determines how far through the file we advance the cursor each time. If we’ve already read data in, unless we close and re-open the file (or use some different functions that we won’t talk about here), we won’t be able to read it again.
The file.readline()
function advances our cursor one line at a time.
# TODO: run me!
# What happens if you try to readline when you've
# already reached the end of the file?
f = open("course_notes_demo.txt", "r")
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
f.close()