Python 3 File Methods
Python 3 File Methods
open() Method
Python The open() method is used to open a file and return a file object. This function is required for all file operations. If the file cannot be opened, an OSError will be raised.
Note: When using the open() method, be sure to close the file object by calling the close() method.
The open() function generally accepts two parameters: the file name (file) and the mode (mode).
open(file, mode='r')
The complete syntax is:
open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Parameter Description:
- file: Required, file path (relative or absolute).
- mode: Optional, file opening mode.
- buffering: Set buffering.
- encoding: Typically uses utf8.
- errors: Error level.
- newline: Distinguish newlines.
- closefd: Type of the file parameter passed in.
- opener:
Mode parameters include:
Mode | Description |
---|---|
t | Text mode (default). |
x | Write mode. Creates a new file. An error will be reported if the file already exists. |
b | Binary mode. |
+ | Open a file for updating (read and write). |
U | Universal newlines mode (not supported in Python 3). |
r | Open a file for reading only. The file pointer will be 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 will be placed at the beginning of the file. This is the default mode. This is commonly used for non-text files such as images. |
r+ | Opens a file for both reading and writing. The file pointer will be placed at the beginning of the file. |
rb+ | Opens a file in binary format for both reading and writing. The file pointer will be placed at the beginning of the file. This is commonly used for non-text files such as images. |
w | Opens a file for writing only. If the file already exists, it will be opened and edited from the beginning, i.e., the existing content will be deleted. If the file does not exist, a new file will be created. |
wb | Opens a file in binary format for writing only. If the file already exists, it is opened and edited from the beginning, deleting any existing content. If the file does not exist, a new file is created. This is commonly used for non-text files such as images. |
w+ | Opens a file for reading and writing. If the file already exists, it is opened and edited from the beginning, deleting any existing content. If the file does not exist, a new file is created. |
wb+ | Opens a file in binary format for reading and writing. If the file already exists, it is opened and edited from the beginning, deleting any existing content. If the file does not exist, a new file is created. This is commonly used for non-text files such as images. |
a | Opens a file for appending. If the file already exists, the file pointer is placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing. |
ab | Opens a file for appending in binary format. If the file already exists, the file pointer is placed at the end of the file. That is, the new content will be written after the existing content. If the file does not exist, a new file is created for writing. |
a+ | Opens a file for reading and writing. If the file already exists, the file pointer is placed at the end of the file. 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 for appending in binary format. If the file already 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. |
The default is text mode. To open in binary mode, add b.
File Object
File objects are created using the open function. The following table lists commonly used functions for file objects:
Number | Method and Description |
---|---|
1 |
file.close() Closes a file. Once closed, the file cannot be read or written. |
2 |
file.flush() Flushes the file’s internal buffer, writing data immediately to the file instead of passively waiting for the output buffer to be filled. |
3 |
file.fileno() Returns an integer file descriptor (FD integer), which can be used in low-level operations such as the read method of the os module. |
4 |
file.isatty() Returns True if the file is connected to a terminal device, otherwise returns False. |
5 |
file.next() File objects in Python 3 do not support the next() method. Return the next line of the file. |
6 |
file.read([size]) Read the specified number of bytes from the file, or all of it if not given or negative. |
7 |
file.readline([size]) Read an entire line, including the “n” character. |
8 |
file.readlines([sizeint]) Read all lines and return a list. If sizeint > 0, return lines totaling approximately sizeint bytes. The actual number read may be larger than sizeint due to buffer padding. |
9 |
file.seek(offset[, whence]) Move the file read pointer to the specified position. |
10 |
file.tell() Return the current file position. |
11 |
file.truncate([size]) Truncates the file starting from the first character of the first line, truncating to size |
12 |
file.write(str) Writes a string to a file, returning the length of the string written. |
13 |
file.writelines(sequence) Writes a list of sequential strings to a file. If newlines are required, add newlines to each line. |