Python informer

Improve your Python coding skills

Files

Introduction

In this lesson we will look at how to use files in Python:

  • Using files
  • Reading data
  • Writing data
  • Using with statements

Using files

There are 3 stages to reading or writing a file in Python:

Opening the file

You open a file by calling the open function. When you open a file you specify:

  • the path of the file, that is the name and the folder of the file.
  • whether you want to read the file, create a new file and write to it.

The open function checks whether the file exists, and whether you are allowed to access the file (for example, you might not be allowed to access a file if it is already open in another program).

If all goes well, the open function returns a file object which you can use to access the file.

Reading and writing data

Python provides various function to read and write data to a file. These functions make use of the file object to access the file.

Closing the file

When you have finished with a file, it is very important to close it. This tells the operating system that you are not using it any more. We will see how to use with statements to ensure the file is always closed at the end.

Reading data

Here is the code to read from a text file, and print out the contents:

f = open('myfile.txt', 'r')
s = f.read()
print(s)
f.close()

Code for reading a file

The first line opens the file. This takes two parameters, the name of the file, and the second parameter is the mode.

The mode can take various values, but the simplest are r, which opens an existing text file for reading, and w which creates a new text file for writing.

The code returns a file object which we store in variable f:

f = open('myfile.txt', 'r')

Now we can read from the file. The read function reads the entire file into one string, which we will store in variable s:

s = f.read()

Here we print the string in s (ie the contents of the file):

print(s)

Finally (and importantly) we close the file:

f.close()

If you want to try this, you should first create a text file called myfile.txt, and add a few lines of text to it. Store it in the same folder as your Python files.

Reading lines from a file

If you have a long text file, you might not want to read the whole file in one go - it could take too long, and use up too much memory.

Here is an alternative:

f = open('myfile.txt', 'r')
s = f.readline()
print(s)
f.close()

readline just reads a single line from a file. To read the entire file, you must call readline multiple times, for example in a loop.

readline returns a line of text, with a newline character (\n) at the end. If there is an empty line in the file, readline will return a string containing just one character, the \n.

When the end of the file is reached, readline will return an empty string.

Looping over the lines in a file

You could set up a loop using readline, but in fact there is an easier way:

f = open('myfile.txt', 'r')
for s in f:
    print(s)
f.close()

We can use the file object as an iterator, in a for loop. The loop will execute once for each line in the file, and stop when it reaches the end of the file. Each time through the loop, the loop variable s will contain the next line from the file.

Writing data

Here is the code to write data to a file:

f = open('newfile.txt', 'w')
s = f.write('Line1\n')
s = f.write('Line2\n')
s = f.write('Line3\n')
f.close()

Code to write a file

To write a file, we use the open function as before, but for the mode (the second parameter) we use w. The open function will create a new file for writing. If the file already exists, it is truncated (any data alreday in the file is removed).

f = open('newfile.txt', 'w')

To write data, we use the write function. It is important to remember to add a new line (\n) character after each line.

s = f.write('Line1\n')

We close the file as before:

f.close()

The write function

There are several different functions for reading data from a file, but only one write function. That is because we can write as much, or as little, as we want with each write call.

We can write line by line:

s = f.write('Line1\n')
s = f.write('Line2\n')
s = f.write('Line3\n')

Or we can write the whole file at once:

s = f.write('Line1\nLine2\nLine3\n')

We can even write parts of a line:

s = f.write('Line1\nLi')
s = f.write('ne2\nLine3\n')

However we split the data, it will all get joined together again when we write the file. The important thing is that we must put a \n at the end of each line.

Using with statements

In the examples so far we have always added a line to close the file:

f = open('newfile.txt', 'w')
s = f.write('Line1\n')
s = f.write('Line2\n')
s = f.write('Line3\n')
f.close()

Unfortunately, file functions can fail for various reasons, such as a read or write error, or the disk being full. In those cases, the program might throw an error before it closes the file. The program might exit leaving the file open.

Closing files is very important because:

  • If you keep the file open, other programs might be blocked from using that file.
  • The operating system might have a limit on how files can be open at any one time. If your program keeps lots of files open for no good reason, it might stop other programs working.
  • When you call the write function, the system might not actually write the data to file straight away. It might buffer the data before writing it out, waiting until there is a larger amount of data that can be written out in one go. When you close the file, all buffered data is written to the file before it is closed.

We can avoid these problems using a with statement.

with open('newfile.txt', 'w') as f:
    s = f.write('Line1\n')
    s = f.write('Line2\n')
    s = f.write('Line3\n')

The with statement acts as a sort of guardian of the file object created by the open function. The as part of the statement is where we decide what we want to call the file object. In this case it is f, but you can use any variable name.

with statements have a body, just like if statements. The body is indented, as you would expect. You can use the file object f normally within the body of the with statement.

But when your code exits the body of the with , for any reason, the file object f will be closed. This happens if the code runs normally, or if any kind of error occurs.

Notice that we don’t need to call the close function at all. It is done automatically in every case.