We’ve talked about importing Python libraries and using them in our programs. But real-world programs also interact with all kinds of files: text, data, images, and more. Many of these file types are challenging to handle without algebra and higher math. Text files, however, are much easier to work with.

There are three major steps to processing any file:

  1. opening the file so that the program can use it
  2. interacting with the file’s contents
  3. closing the file when we’re done with it

On a coding level, there are two different modes of interacting with a file: reading information from the file, or writing (sending) information to it. When you use word processing software and other applications, the interface that allows you to “edit” a file is actually reading and writing data in a complicated sequence.

When you want to get information from a file, including just looking at the file's contents, you're reading from the file. Python has a function that handles both reading and writing files. A file can be "imported" quite easily by assigning it to a variable:

textFile = open('fileName.txt', 'r')

We're using the open() function, which takes two parameters. The first parameter is a string containing the file name (and its extension). The second parameter is an 'r' or a 'w', and tells Python whether we are reading information from the file or writing, or sending, information to it.

Right now, textFile is a file object containing a long string of text. We want to extract the text so that we can interact with it more easily. There are a few different ways to do this, depending on whether you want to put all of the text into one string or separate it out into multiple strings.

The .read() function will return the full contents of the file as a single string. The .readlines() function will split the contents of the file wherever it finds a line break, returning a list of strings.

textBlock = textFile.read()
textLines = textFile.readlines()

Both textBlock and textlines contain the entire contents of textFile, just in slightly different formats. You can choose which approach to use based on what you're planning on doing with the text.

Once you're done extracting the contents of a file, you should close the file to make sure that the it's stored properly for future use.

textFile.close()

Use the outline in the program below to extract the contents of the file movieRankings.txt using the readlines() function. The file lists the top ten highest grossing animated films, highest first. Once you've extracted the contents, print the 5th film in the console.

Next, we'll learn how to send information to a file.

When you want to save information to a file, including changing the contents of an existing file, what you're doing is writing to the file. This starts by using the same open() function as for reading files:

newFile = open('outputFile.txt', 'w')

Notice, though, that this time we use a 'w' as the second parameter, instead of an 'r'. When you run the open function, it creates a new, empty file with the name you've entered.

Important note: Writing is not a way to quickly edit an existing file. If there's an existing file with that name, it will be deleted and replaced with an empty file!

To write information to a text file, we can use the print function, adding an extra parameter for the destination:

print('I created this text file with Python.', file=newFile)

This will send the string to outputFile.txt instead of printing it in the console. Additional print statements to the same file will place the new string after previous ones.

When you're done writing to a file, it's especially important to close the file so your data gets saved!

newFile.close()

Now you're going to add to animated movie program. Create a variable that holds the value of the top grossing animated film. Then create an output file and write a sentence stating what the highest grossing film is. Don't forget to close your file at the end!