< BACKMake Note | BookmarkCONTINUE >
156135250194107072078175030179198180024228156016206217188240240204175204055039071133032084

File Built-in Function [open()]

As the key to opening file doors, the open() built-in function provides a general interface to initiate the file input/output (I/O) process. open() returns a file object on a successful opening of the file or else results in an error situation. When a failure occurs, Python generates or raises an IOError exception—we will cover errors and exceptions in the next chapter. The basic syntax of the open() built-in function is:

					
file_object = open(file_name, access_mode='r', buffering=-1)

				

The file_name is a string containing the name of the file to open. It can be a relative or absolute/full pathname. The access_mode optional variable is also a string, consisting of a set of flags indicating which mode to open the file with. Generally, files are opened with the modes "r," "w," or "a," representing read, write, and append, respectively.

Any file opened with mode "r" must exist. Any file opened with "w" will be truncated first if it exists, and then the file is (re)created. Any file opened with "a" will be opened for write. If the file exists, the initial position for file (write) access is set to the end-of-file. If the file does not exist, it will be created, making it the same as if you opened the file in "w" mode. If you are a C programmer, these are the same file open modes used for the C library function fopen().

There are other modes supported by fopen() that will work with Python's open(). These include the "+" for read-write access and "b" for binary access. One note regarding the binary flag: "b" is antiquated on all Unix systems which are POSIX-compliant (including Linux) because they treat all files as "binary" files, including text files. Here is an entry from the Linux manual page for fopen(), which is from where the Python open() function is derived:

The mode string can also include the letter "b" either as a last character or as a character between the characters in any of the two-character strings described above. This is strictly for compatibility with ANSI C3.159-1989 ("ANSI C") and has no effect; the "b" is ignored on all POSIX conforming systems, including Linux. (Other systems may treat text files and binary files differently, and adding the "b" may be a good idea if you do I/O to a binary file and expect that your program may be ported to non-Unix environments.)

You will find a complete list of file access modes, including the use of "b" if you choose to use it, in Table9.1. If access_mode is not given, it defaults automatically to "r."

The other optional argument, buffering, is used to indicate the type of buffering that should be performed when accessing the file. A value of 0 means no buffering should occur, a value of 1 signals line buffering, and any value greater than 1 indicates buffered I/O with the given value as the buffer size. The lack of or a negative value indicates that the system default buffering scheme should be used, which is line buffering for any teletype or tty-like device and normal buffering for everything else. Under normal circumstances, a buffering value is not given, thus using the system default.

Table 9.1. Access Modes for File Objects
File Mode Operation
r open for read
w open for write (truncate if necessary)
a open for write (start at EOF, create if necessary)
r+ open for read and write
w+ open for read and write (see "w" above)
a+ open for read and write (see "a" above)
rb open for binary read
wb open for binary write (see "w" above)
ab open for binary append (see "a" above)
rb+ open for binary read and write (see "r+" above)
wb+ open for binary read and write (see "w+" above)
ab+ open for binary read and write (see "a+" above)

Here are some examples for opening files:

					
fp = open('/etc/motd')        #open file for read
fp = open('test',  'w')       #open file for write
fp = open('data',  'r+')      #open file for read/write
fp = open('c:\io.sys', 'rb')  #open binary file for read

				


Last updated on 9/14/2001
Core Python Programming, © 2002 Prentice Hall PTR

< BACKMake Note | BookmarkCONTINUE >

© 2002, O'Reilly & Associates, Inc.