File I/O
Just as a string object in Python is defined by the str class, a file object is defined by the file class. Instances of the file class have several methods available for performing typical operations such as reading and writing.
Opening a file
To access a specific file in the file system, you must create a file object and associate the correct filename to it. This file object may be instantiated with the open() function. Usually open() takes two arguments, the filename and the access mode. Thus, the open() function has the following syntax:
fileobj = open('filename.file',mode)
The specified file—in our example, 'filename.file'—does not necessarily have to exist in the file system; it depends on the access mode. For modes that are not primarily Read modes, a new file will be created, if a file with the corresponding name does not exist. The outcome of subsequent file actions also depends on the access mode. The following is a list of the available access modes; details are described in the various columns.
Mode | Name | File Status | Starts At | Position of Writes |
---|---|---|---|---|
'r' | Read Only | File must exist | Beginning | Writing not allowed |
'w' | Write Only | File is created OR EMPTIED | Beginning (= end) | Writes (after first) may be positioned with seek |
'a' | Append Only | File is created or exists | End | Writes ALWAYS move to the end of the file |
'r+' | Read and Write | File must exist | Beginning | Writes (after first) may be positioned with seek |
'w+' | Write and Read | File is created OR EMPTIED | Beginning (= end) | Writes (after first) may be positioned with seek |
'a+' | Append and Read | File is created or exists | Beginning | Writes ALWAYS move to the end of the file |
In brief, these modes work pretty much as they do with the C fopen() function. Python 3 adds new modes to the above.
Reading from a file
As you might expect, the basic method that reads data from a file object f is named f.read(). What you might not expect is its exact behavior. Let’s say that you have a file “test.txt” containing the following sentence, one word per line, no spaces: "This is a file." Here’s how f.read() would read it:
Notice that f.read() reads every character in the file, including the new-line characters \n. If you try to re-read the file right away, you will come up empty! To read your file again, you must either call f.seek(0) to move the file position back to the beginning, or close the file and re-open it:
If you want to read just one line at a time, use the method f.readline() instead.
You can also read the entire file, storing each line in a different element in an array, by using the function f.readlines(). Notice the plural: it is NOT the same as f.readline(). And if you don't know how big the file is, you might not want to read everything into memory at once.
It is also possible to iterate through a file with a for loop:
We use str.rstrip()
to remove a newline from the end of each line, as print will add a newline itself. The related methods str.strip()
and str.lstrip()
from the str
class can also be useful.
Instead of iterating through the file with a for loop, you can fill a list with lines using the file.readlines()
method and then iterate through the list:
And as an alternative to using f.readlines()
, you could read the entire contents of the file into a string object using f.read()
as above, and then use the splitlines()
method on the string to split out each line separately, creating a list of single-line strings. The advantage of this approach is that str.splitlines()
strips the newline characters from each line, unlike file.readlines()
.
Writing to a file
Yes, you guessed it: f.write(string) writes the contents of string to the file associated with f. Subsequent calls to f.write() will continue to append output to the file until f.close() is called. Any output to f is appended to the same line until a new-line character is encountered.
In Python 3, each f.write() call also returns an integer, which is the number of characters that were written to the file.
Closing a file
Once everything is done, remember to close the file using f.close(). It takes no arguments. This method will free any system resources taken by the open file.
By the way, when you call a method with no arguments like f.close(), you must remember to include the parentheses. If you omit them, Python will just print some quick information on the method without actually executing it.
Managing file access using with
Because an open file must be closed — even though closing typically takes place after multiple intermediate statements — a better approach to managing file access involves with the Python keyword with
. A with
statement is used to wrap the execution of a block with methods defined by what is known as a context manager. While the usage of with
is broader than just file I/O, the place it is most often seen involves file opening and closing. By enclosing a file open
command within a with
block, programmers can ensure that the file will be closed upon termination of the block, even without a corresponding close
call. Echoing the examples above, the following code opens, reads, prints, and closes the file.