Reading and writing text files

We’ll focus on regular text files. By “regular,” I mean a file that is intended to be a text file, with text divided into lines. It’s also possible to have files that hold non-text data, but we will not discuss these types of files now.

To read from a regular text file, follow this basic paradigm:

  1. Open the file for reading.
  2. For each line in the file:
    1. Read the line from the file.
    2. Process the line. In other words, do whatever you need to do with the line.
  3. Close the file.

To open a file for reading, call the built-in Python function open. It takes two parameters, both strings. The first parameter gives the name of the file. The second parameter should be the string "r", which indicates that you’re reading from the file. The function returns a reference to an object representing the file. (I don’t know what kind of object it is, nor do I care. Lovin’ that abstraction! I’ll call it a “file object,” even though I don’t know what kind of object it really is.) For example, to open the file moby_dick.txt for reading:

file_name = "moby_dick.txt"
in_file = open(file_name, "r")

Of course, you could get that down to a single line:

in_file = open("moby_dick.txt", "r")

To read lines from the file, you use a for-loop:

for line in in_file:
    # Do something with line

Here, in each iteration of the loop, the variable line will be a line from the file. Notice that this type of for-loop differs from the for-loops we’ve already seen, in that what follows in is not a list, but a reference to a file object.

In a file, each line ends with a newline character, which we indicate in Python by "\n". Each line that you read from the file will be a string containing all the characters in the line, including the newline character. For example, if you open the file states.txt and print line in the first iteration of the for-loop, you’ll print

Maine, New Hampshire, Vermont, Massachusetts

with a newline at the end.

The following program reads each line of the file and writes it to the console. It relies on states.txt being in the same directory as the Python program.

Depending on your version of Python (version 2 or version 3) or your operating system, there might be a special character at the end of the line you input representing the newline character. In the interactive example, there is not.

When you’re all done reading from a file, you must close it. (Your mother told you to close something you’d opened when you were done with it, right? I know my mother told me.) To close a file, you call the close method on the object that open returned:


Notice that close is a method, a function that works on an object and is called using dot notation, but open is not. Why? Because you don’t have a file object until open runs; it’s open that returns you the reference to the file object. When you’re done with the file, you can call the close method on the file object.

There’s nothing special about the identifiers in_file and line. You may use whatever name you like for the reference to the file object and for the string that is read from each line of the file.

Note that if you are going to open a file, read from it or write into it, and then open it again to start at the beginning, you must close it before reopening.

How to write to a file

Writing to a file is similar to reading from a file, but it differs in some important ways.

First, you have to open the file for writing. The second parameter to the open function shouldn’t be "r". Instead, you have your choice of "w" or "a". If you want to append to a file—that is, add to the end of an existing file—then choose "a". If you want to overwrite the file if it already exists, choose "w". If the file doesn’t exist and you choose "a", you get the same effect as choosing "w". So, for example:

out_file = open(file_name, "w")

There’s nothing special about the name out_file; use whatever name you like.

Just as you close a file that you’re reading when you’re done reading from it, you must close a file that you’re writing when you’re done writing to it. You call the close method just as you call it for reading:


To write to a file, call the write method on the file. This method takes one parameter, a string, and it writes to the end of the file. If you want a newline in the file, you have to put it into the string yourself.

Here is an example of writing a file:

The run button is disabled since the Python version of the browser can read files, but cannot write them, for security reasons.

On a more typical version of Python, the code produces the file gettysburg.txt within the project folder:

Four score and seven years ago,
our fathers brought forth on this continent, a new nation,
conceived in Liberty, and dedicated to the proposition that all men are created equal.

Of course, you may call the write method within a loop.