Home > Backend Development > Python Tutorial > How to operate files in python

How to operate files in python

silencement
Release: 2022-06-07 15:15:38
Original
12126 people have browsed it

How to operate files in python

open() method

Python open() method opens a file and returns a file object, which is required during file processing. Using this function, if the file cannot be opened, OSError will be thrown.

Note: When using the open() method, you must ensure that the file object is closed, that is, the close() method is called.

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

open(file, mode='r')
Copy after login

The complete syntax format is:

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
Copy after login

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 newlines

closefd: Incoming file parameter type

mode parameters are:

mode Description
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 (readable and writable).
U Universal newline mode (not recommended).
#r Open the file read-only. The file pointer will be placed at the beginning of the file. This is the default mode.
rb Open a file in binary format for reading 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, 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 pictures, etc.
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 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. Generally used for non-text files such as pictures, etc.
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, etc.
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, new content will be written after 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, new content will be written after 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, a new file is created 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, a new file is created for reading and writing.

The default is text mode. If you want to open it in binary mode, add b.

The above is the detailed content of How to operate files in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template