Python File method

open() method

The Python open() method is used to open a file and return a file object. This function is required to process the file. If the file cannot be opened, an OSError will be thrown.

Note: Use the open() method to ensure that the file object is closed, ie the close() method is called.

The usual form of the

open() function is to receive two parameters: the file name (file) and the mode (mode).

open(file, mode=' r')

The full 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 path).
  • mode: optional, file open mode
  • buffering: setting buffer
  • encoding: generally use utf8
  • errors: error level
  • newline: distinguish line breaks
  • closefd: type of file parameter passed in
  • opener:
The

mode parameter is:

ModeDescription
tText mode (default).
xWrite mode, create a new file, and report an error if the file already exists.
bbinary mode.
+Open a file for updating (readable and writable).
UUniversal Wrap Mode (not recommended).
rOpen the file as read-only. The pointer to the file will be placed at the beginning of the file. This is the default mode.
rbOpens 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 pictures.
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 pictures.
wOpen a file for writing only. If the file already exists, open the file and start editing from the beginning, ie the original content will be deleted. If the file does not exist, create a new one.
wbOpen a file in binary format for writing only. If the file already exists, open the file and start editing from the beginning, ie the original content will be deleted. If the file does not exist, create a new one. Generally used for non-text files such as pictures.
w+Open a file for reading and writing. If the file already exists, open the file and start editing from the beginning, ie the original content will be deleted. If the file does not exist, create a new one.
wb+Opens a file in binary format for reading and writing. If the file already exists, open the file and start editing from the beginning, ie the original content will be deleted. If the file does not exist, create a new one. Generally used for non-text files such as pictures.
aOpen a file for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to the existing content. If the file does not exist, create a new file for writing.
abOpens a file in binary format for appending. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written to 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. Append mode will be used when the file is opened. 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, and the following table lists the functions commonly used by the file object:






welookups is optimized for learning.© welookups .
All Right Reserved and you agree to have read and accepted our term and condition