File Built-in Function fopen()

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. 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 pened for write. If the file exists, the initial position for file (write) access is set to the end-offile. 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 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.
 

Mode Operation Performed
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)

Examples

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