Home > Article > Backend Development > How to read and write files in Python
How to read and write files in Python
Files are one of the important tools for us to store and process data. In Python, data input and output can be realized through file read and write operations, which facilitates us to analyze, process and store data. This article will introduce how to read and write files in Python and provide specific code examples.
Before performing file reading and writing operations, we need to open the file first. You can use the open()
function to open a file and specify the opening mode (read, write, append, etc.). The following is a code example for opening a file:
file = open('file.txt', 'r')
In the above code, file.txt
is the name of the file to be opened, 'r'
means opening in read mode document. After opening the file, we can read or write to the file.
Reading file content is a common file operation. In Python, we can use the read()
function to read the contents of a file. The following is a code example for reading the contents of a file:
file = open('file.txt', 'r') content = file.read() print(content) file.close()
In the above code, the read()
function reads the contents of the file into the content
variable and uses print()
Function output content. After reading is completed, the file needs to be closed using the close()
function.
In addition to using the read()
function to read the contents of the entire file at once, we can also use the readline()
function to read the contents of the file line by line. The sample code is as follows:
file = open('file.txt', 'r') line = file.readline() while line: print(line) line = file.readline() file.close()
In the above code, we use the readline()
function to read the contents of the file line by line. When the end of the file is encountered, readline()
The function will return an empty string and the loop ends.
In addition to reading the contents of files, Python also provides the function of writing files. We can use the write()
function to write data to a file. The following is a code example for writing file content:
file = open('file.txt', 'w') file.write('Hello, World!') file.close()
In the above code, we use the write()
function to write the string 'Hello, World!'
document. After writing is completed, the file needs to be closed using the close()
function. When writing to a file, if the file already exists, the original content will be overwritten; if the file does not exist, a new file will be created.
Sometimes we need to append new content to the end of the file instead of overwriting the original content. In Python, we can use the 'a'
mode of the open()
function to append the file content. The sample code is as follows:
file = open('file.txt', 'a') file.write('Hello, Python!') file.close()
In the above code, we use the 'a'
mode to open the file and append the string 'Hello, Python!'
to the file. end.
Using the with
statement can simplify file opening and closing operations. In the with
statement block, we can directly use the file object for reading or writing operations without explicitly calling open()
and close()
function. The sample code is as follows:
with open('file.txt', 'r') as file: content = file.read() print(content) with open('file.txt', 'w') as file: file.write('Hello, World!') with open('file.txt', 'a') as file: file.write('Hello, Python!')
In the above code, we use the with open()
statement to open the file, and read or read the file in the with
statement block write operation. After the with
statement block ends, the file will be automatically closed.
Summary:
File reading and writing operations in Python are very flexible and convenient. You can use the open()
function to open the file and read()
Function and write()
function performs read and write operations. In addition, you can also use the readline()
function to read the contents of the file line by line, and use the 'a'
mode to append the contents of the file. To simplify file operations, we can use the with
statement block to automatically close the file. By rationally using these operations, we can better perform file reading and writing operations and data processing.
The above is the detailed content of How to read and write files in Python. For more information, please follow other related articles on the PHP Chinese website!