In this lesson we will look at how to use files in Python:
There are 3 stages to reading or writing a file in Python:
You open a file by calling the open function. When you open a file you specify:
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.
Python provides various function to read and write data to a file. These functions make use of the file object to access 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.
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()
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, andw
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.
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.
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.
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()
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()
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.
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:
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.
If you found this article useful, you might be interested in the book Functional Programming in Python, or other books, by the same author.
<<PrevCopyright (c) Axlesoft Ltd 2020