next up previous contents
Next: Methods for Reading Up: Input and Output Previous: Using Names in Format   Contents


File Objects

In python, access to files is provided through a file object, which is created by calling the builtin open function. Once such an object is created, a large number of methods are available for accessing the file, both for reading and writing.

The open function takes between one and three arguments. The first, which is the only required argument, is the name of the file to open. The second is a string which describes the action(s) that you plan to do to the file, as illustrated in Table 5.2. If you do not provide a second argument, the default is 'r', that is, the file is opened for reading. A 'b' can be appended to the second argument to indicate that the file to be opened is a binary file as opposed to a text file; this is only meaningful under operating systems like Windows which make a distinction between text files and binary files, and, when omitted, often causes problems when porting a program to such an operating system. Finally, the optional third argument specifies information about how the file should be buffered; a value of 0 means no buffering, a value of 1 means line buffering, and any other positive value indicates the size of the buffer to be used. (In this context, the buffer controls the amount of information that is manipulated in memory before any action is actually taken on the file.) In most cases, there should be no need to provide open with a third argument. Unfortunately, there is no way to unbuffer standard output inside an executing program; if you need standard output to be unbuffered, you must invoke python with the -u flag.

When specifying a filename under Windows, you can use a single backslash (\), a double backslash (\\), or a single forward slash (/) as a file separator. A single backslash should be used with caution, because all of the backslashed sequences in Table 2.1 will still retain their usual meaning.


Table 5.2: File modes for the open function
String Meaning
r Open file for reading; file must exist.
w Open file for writing; will be created if it doesn't exist
a Open file for appending; will be created if it doesn't exist
r+ Open file for reading and writing; contents are not destroyed
w+ Open file for reading and writing; contents are destroyed
a+ Open file for reading and writing; contents are not destroyed


If python encounters a problem when opening a file, it will issue an IOError exception, along with a more detailed message describing the problem. Thus, it is a wise practice to open files inside a try/except clause to prevent your program from terminating ungracefully when there's a problem opening a file.

Once you have successfully opened a file, there are a number of methods which can be invoked by the file object returned by open. The following subsections describe some of these methods.



Subsections
next up previous contents
Next: Methods for Reading Up: Input and Output Previous: Using Names in Format   Contents
Phil Spector 2003-11-12