Python open() Function
The Python open() function opens a file and creates a file object, whose related methods can then be called to read from or write to it.
For more file operations, please refer to:Python File I/O.
Function syntax
open(name[, mode[, buffering]])
Parameter Description:
-
name : a string value containing the name of the file you want to access.
-
mode : determines the mode in which the file is opened: read-only, write, append, etc. See the complete list of possible values below. This parameter is optional; the default file access mode is read-only (r).
-
buffering : if buffering is set to 0, no buffering occurs. If buffering is set to 1, lines are buffered when accessing the file. If buffering is set to an integer greater than 1, that value specifies the buffer size. If a negative value is given, the buffer size uses the system default.
The complete list of modes for opening files:
| Mode | Description |
|---|---|
| t | Text mode (default). |
| x | Write mode, creates a new file; if the file already exists, an error is raised. |
| b | Binary mode. |
| + | Opens a file for updating (both reading and writing). |
| U | Universal newline mode (not recommended). |
| r | Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode. |
| rb | Opens a file in binary format for reading only. The file pointer is placed at the beginning of the file. This is the default mode. Generally used for non-text files such as images. |
| r+ | Opens a file for both reading and writing. The file pointer is placed at the beginning of the file. |
| rb+ | Opens a file in binary format for both reading and writing. The file pointer is placed at the beginning of the file. Generally used for non-text files such as images. |
| w | Opens a file for writing only. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. |
| wb | Opens a file in binary format for writing only. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. Generally used for non-text files such as images. |
| w+ | Opens a file for both reading and writing. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. |
| wb+ | Opens a file in binary format for both reading and writing. If the file exists it is opened and editing starts from the beginning, so the original contents are deleted. If the file does not exist, a new file is created. Generally used for non-text files such as images. |
| a | Opens a file for appending. If the file exists, the file pointer is placed at the end of the file, so new content is written after the existing content. If the file does not exist, a new file is created for writing. |
| ab | Opens a file in binary format for appending. If the file exists, the file pointer is placed at the end of the file, so new content is written after the existing content. If the file does not exist, a new file is created for writing. |
| a+ | Opens a file for both reading and writing. If the file exists, the file pointer is placed at the end of the file and the file is opened in append mode. If the file does not exist, a new file is created for reading and writing. |
| ab+ | Opens a file in binary format for appending. If the file exists, the file pointer is placed at the end of the file. If the file does not exist, a new file is created for reading and writing. |
file object methods
-
file.read([size]): If size is not specified, the entire file is returned. If the file size is more than twice the memory, this can be a problem. f.read() returns "" (empty string) when the end of the file is reached.
-
file.readline(): Returns one line.
-
file.readlines([size]) : Returns a list containing size lines. If size is not specified, all lines are returned.
-
for line in f: print line : Access through an iterator.
-
f.write("hello\n"): To write data other than strings, first convert it to a string.
-
f.tell(): Returns an integer indicating the current position of the file pointer (the number of bytes from the beginning of the file).
-
f.seek(offset, [starting position]): Used to move the file pointer.
- offset: measured in bytes, can be positive or negative
- start position: 0 - beginning of file, default; 1 - current position; 2 - end of file
-
f.close() Closing a File
For more information, refer to:Python File methods
Example
The test file test.txt has the following contents:
RUNOOB1
RUNOOB2
f
=
open
(
'
test.txt
'
)
f
.
read
(
)
'
RUNOOB1
\n
RUNOOB2
\n
'
Python built-in function