home PYTHONJAVA
 

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: Set buffer
  • encoding: 一Set buffer
  • errors: Error level
  • newline: Distinguish line breaks
  • closefd: Incoming file parameter type
  • 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. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.