YouTip LogoYouTip

File Methods

# Python2.x Python File Methods ### open() Method The Python open() method is used to open a file and return a file object. This function is required in the process of processing files. If the file cannot be opened, an OSError will be thrown. **Note:** When using the open() method, be sure to close the file object, i.e., call the close() method. The common form of the open() function is to receive two parameters: filename (file) and mode (mode). open(file, mode='r') The complete syntax format 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 path). * mode: Optional, file opening mode * buffering: Set buffering * encoding: Generally use utf8 * errors: Error level * newline: Distinguish line breaks * closefd: The type of the file parameter passed in * opener: Set a custom opener, the return value of the opener must be an open file descriptor. The mode parameters are: | Mode | Description | | --- | --- | | t | Text mode (default). | | x | Write mode, create a new file, if the file already exists, an error will be reported. | | b | Binary mode. | | + | Open a file for updating (readable and writable). | | U | Universal newline mode (not recommended). | | r | Open file in read-only mode. The file pointer will be placed at the beginning of the file. This is the default mode. | | rb | Open a file in binary format for read-only. The file pointer will be placed at the beginning of the file. This is the default mode. Generally used for non-text files such as images. | | r+ | Open a file for reading and writing. The file pointer will be placed at the beginning of the file. | | rb+ | Open a file in binary format for reading and writing. The file pointer will be placed at the beginning of the file. Generally used for non-text files such as image files. | | w | Open a file for writing only. If the file already exists, open the file and edit from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. | | wb | Open a file in binary format for writing only. If the file already exists, open the file and edit from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as images. | | w+ | Open a file for reading and writing. If the file already exists, open the file and edit from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. | | wb+ | Open a file in binary format for reading and writing. If the file already exists, open the file and edit from the beginning, i.e., the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as images. | | a | Open a file for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, new content will be written after the existing content. If the file does not exist, create a new file for writing. | | ab | Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. That is, new content will be written after the existing content. If the file does not exist, create a new file for writing. | | a+ | Open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. The file will be opened in append mode. If the file does not exist, create a new file for reading and writing. | | ab+ | Open a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing. | The default is text mode. If you want to open in binary mode, add b. ### file Object The file object is created using the open function. The following table lists the commonly used functions of the file object: | No. | Method and Description | | --- | --- | | 1 | [file.close()](#) Close the file. The file cannot be read or written after closing. | | 2
← File CloseJsref Some β†’