Python open() Function

Python 内置函数Python Built-in Functions

The python open() function is used to open a file, creating afileobject, so that related methods can be called on it to read and write.

For more file operations, 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: mode determines the mode in which the file is opened: read-only, write, append, etc. See the complete list of all possible values below. This parameter is optional; the default file access mode is read-only (r).

  • buffering: If the value of buffering is set to 0, no buffering will occur. If the value of buffering is set to 1, lines are buffered when accessing the file. If the value of buffering is set to an integer greater than 1, it indicates the buffer size of the buffer area. If a negative value is used, the buffer size is the system default.

Complete list of different modes for opening files:

ModeDescription
tText mode (default).
xWrite mode, creates a new file; an error will be reported if the file already exists.
bBinary mode.
+Open a file for updating (readable and writable).
UUniversal newline mode (not recommended).
rOpen a file in read-only mode. The file pointer will be placed at the beginning of the file. This is the default mode.
rbOpen 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 images.
wOpen a file for writing only. If the file already exists, open the file and edit from the beginning, meaning the original content will be deleted. If the file does not exist, create a new file.
wbOpen a file in binary format for writing only. If the file already exists, open the file and edit from the beginning, meaning 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, meaning 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, meaning 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.
aOpen 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.
abOpen 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.

File Object Methods

  • file.read([size]): If size is not specified, the entire file is returned. If the file size is greater than twice the memory, there will be a problem. f.read() returns "" (empty string) when it reads to the end of the file.

  • file.readline(): Returns a line.

  • file.readlines([size]) : Returns a list containing size lines; if size is not specified, returns all lines.

  • for line in f: print line : Access via iterator.

  • f.write("hello\n"): If you want to write data other than a string, first convert it to a string.

  • f.tell(): Returns an integer indicating the current file pointer position (that is, the number of bytes from the beginning of the file).

  • f.seek(offset, [start_position]): Used to move the file pointer.

    • Offset: in bytes, can be positive or negative.
    • Start position: 0 - beginning of file, default value; 1 - current position; 2 - end of file
  • f.close()Close file

For more content, refer to:Python File (file) Methods

Examples

Test file test.txt, content as follows:

EXAMPLE1
EXAMPLE2
>>>f = open('test.txt') >>> f.read() 'EXAMPLE1\nEXAMPLE2\n'

Python 内置函数Python Built-in Functions

Other Extensions