Python3 File Methods

open() Method

Python open()This method is used to open a file and return a file object.

This function is needed whenever processing files. If the file cannot be opened, it will raiseOSError。

Note:When usingopen()method, you must ensure the file object is closed, that is, call theclose()method.

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

open(file, mode='r')

The full 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 reporting level
  • newline: Distinguish newline characters
  • closefd: The type of the passed file parameter
  • opener: Set a custom opener. The return value of the opener must be an open file descriptor.

The mode parameter has:

ModeDescription
tText mode (default).
xWrite mode. Create a new file; if the file already exists, an error will be raised.
bBinary mode.
+Open a file for updating (readable and writable).
UUniversal newline mode (Not supported in Python 3)。
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, etc.
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, etc.
wOpen a file for writing only. If the file already exists, open it and start editing from the beginning, i.e., 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 it and start editing 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, etc.
w+Open a file for reading and writing. If the file already exists, open it and start editing 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 it and start editing 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, etc.
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 in append mode when 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.

Default is text mode. To open in binary mode, addb 。

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. After closing, the file can no longer be read or written.

2

file.flush()

Flush the internal buffer of the file, directly writing the data in the internal buffer to the file immediately, rather than passively waiting for the output buffer to be written.

3

file.fileno()

Return 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.

4

file.isatty()

Return True if the file is connected to a terminal device, otherwise return False.

5

file.next()

The File object in Python 3 does not support the next() method.

Return the next line of the file.

6

file.read([size])

Read the specified number of bytes from the file; if not given or negative, read all.

7

file.readline([size])

Read an entire line, including the "\n" character.

8

file.readlines([sizeint])

Read all lines and return a list. If sizeint>0 is given, return lines with a total of approximately sizeint bytes. The actual read value may be larger than sizeint because the buffer needs to be filled.

9

file.seek(offset[, whence])

Move the file read pointer to a specified position

10

file.tell()

Return the current position of the file.

11

file.truncate([size])

Truncate from the first character of the first line of the file, truncating the file to size characters. No size means truncating from the current position. After truncation, all subsequent characters are deleted. A newline in the Windows system represents a size of 2 characters.

12

file.write(str)

Write a string to the file, returning the length of the written characters.

13

file.writelines(sequence)

Write a sequence list of strings to the file. If line breaks are needed, you must add the newline character to each line yourself.

Other extensions