Python 3 [File Operation]


open() method


The Python open() method is used to open a file and return the file object. This function is needed in the process of processing the file. Use the open() method to ensure that the file object is closed, that is, call the close() method. The common form of the open() function is to receive two parameters: file name (file) and mode (mode).

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 newline characters

  • closefd: file parameter type passed in

  • opener:

The mode parameters are:


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 update ( read and write ) .

U : Universal line break mode ( Python 3  does not support).

r : Open the file as read-only. The pointer of the file 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 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.

w : Open a file for writing only. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.

wb : Open a file in binary format only for writing. If the file already exists, open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. 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, that is, 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 start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file. Generally used for non-text files such as pictures.

a : Open 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 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. In other words, the 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. When the file is opened, it will be 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


The file object is created using the open function. The following table lists the commonly used functions of the file object:

file.close(): Close the file. The file cannot be read or written after it is closed.

file.flush(): Flush the internal buffer of the file and directly write the data in the internal buffer to the file immediately instead of passively waiting for the output buffer to be written.

file.fileno()::: Returns an integer file descriptor (file descriptor FD integer), which can be used in some low-level operations such as the read method of the os module.

file.isatty(): Returns True if the file is connected to a terminal device, otherwise returns False.

file.read([size]): Read the specified number of bytes from the file, if not given or negative, read all.

file.readline([size]): Read the entire line, including "\n" characters.

file.readlines([sizeint]):读取所有行并返回列表,若给定sizeint>0,返回总和大约为sizeint字节的行, 实际读取值可能比 sizeint 较大, 因为需要填充缓冲区。

file.seek(offset[, whence]):设置文件当前位置

file.tell()::返回文件当前位置。

file.truncate([size]):从文件的首行首字符开始截断,截断文件为 size 个字符,无 size 表示从当前位置截断;截断之后后面的所有字符被删除,其中 Widnows 系统下的换行代表2个字符大小。


file.write(str):将字符串写入文件,返回的是写入的字符长度。

file.writelines(sequence):向文件写入一个序列字符串列表,如果需要换行则要自己加入每行的换行符。




Guess you like

Origin blog.51cto.com/15069490/2578674