Chapter 16. Files

Table of Contents

16.1. Handle files in programs
16.2. Reading data from files
16.3. Writing in files
16.4. Design problems
16.5. Documentation strings

16.1. Handle files in programs

In Section 4.4, we have introduced simple reading and writing file operations. This section provides a more advanced view of the whole set of file operations and attributes.

File data are ordered collections, such as lists. But you have to traverse all elements to get to a position, whereas lists can by accessed directly using an index.

Opening.  When you open a file in Python using the built-in function open, you have to indicate, in addition to its name, whether you want to read from it or write in it. The default is reading.

Working.  The file object created by the open function has methods to read from the file, to write in it and to move the file pointer to another position.

Although Python can handle files containing binary data as well as text data, file objects have special functions to work with text files, such as reading line per line.

At this point we like to show two possibilities to handle text file data line per line. The first one uses the loop while:


while line:
    #do something with the line

And the second one the loop for.

Example 16.1. Reading from files


for line in infh.xreadlines()
    #do something with the line


There exists two file methods, readlines and xreadlines, with the same properties as range and xrange. The first one reads all the lines of a file and puts them in a list, whereas the second one allows to iterate over all lines without creating the list. The first one is appropriated for small files, but if you are not sure about the number of lines in your file, the second one prevents to overload the memory.

Table 16.1 gives a list of all common used file methods and Table 16.2 explains all possible modes to work with files.

Table 16.1. File methods

read([n])reads at most n bytes; if no n is specified, reads the entire file
readline([n])reads a line of input, if n is specified reads at most n bytes
readlines()reads all lines and returns them in a list
xreadlines()reads all lines but handles them as a XRangeType[a]
write(s)writes strings s
writelines(l)writes all strings in list l as lines
close()closes the file
seek(offset [, mode])

changes to a new file position=start + offset. start is specified by the mode argument: mode=0 (default), start = start of the file, mode=1, start = current file position and mode=2, start = end of the file

[a] See Section 11.5 for more informations

Table 16.2. File modes

[rwa]b[reading,writing,append] as binary data (required on Windows)
r+update+reading (output operations must flush their data before subsequent input operations)
w+truncate to size zero followed by writing

Closing.  Although Python closes all opened files when the interpreter exits, it is a good idea to close them explicitly. Why ???


Pay attention, while open is a built in function creating a file object, close is a method of the created file object.