Table of contents
1. File read and write mode
model |
specific information |
r |
The file is read-only, and an error will be reported if the file does not exist |
r+ |
The file is readable and writable. If the file does not exist, an error will be reported, and the original content will be overwritten when writing. |
rb |
The file is read-only (binary format), and an error will be reported if the file does not exist |
rb+ |
The file is readable and writable (binary format), if the file does not exist, an error will be reported, and the original content will be overwritten when writing |
w |
The file is write-only, if the file does not exist, it will be created, and the original content will be overwritten when writing |
w+ |
The file is readable and writable. If the file does not exist, it will be created. When writing, the original content will be overwritten |
wb |
The file is write-only (binary format), if the file does not exist, it will be created, and the original content will be overwritten when writing |
wb+ |
The file is readable and writable (binary format). If the file does not exist, it will be created. When writing, the original content will be overwritten |
a |
The file is only written. If the file does not exist, it will be created. When writing, it will be appended to the end of the file |
a+ |
The file is readable and writable. If the file does not exist, it will be created. When writing, it will be appended to the end of the file |
ab |
The file is write-only (binary format), if the file does not exist, it will be created, and it will be appended to the end of the file when writing |
ab+ |
The file is readable and writable (binary form), if the file does not exist, it will be created, and it will be appended to the end of the file when writing |
To put it simply, the file read and write modes mainly include three types, namely 'r', 'w', and 'a', corresponding to read-only, overwriting and appending. Each major mode can be assisted by using 'b' and '+', 'b' corresponds to the binary form, and '+' corresponds to simultaneous reading and writing. In addition, 'b' and '+' can be used at the same time, so a total of 12 file reading and writing modes can be formed.
Second, read the file
1. Functions used
function name |
meaning |
read() |
Read the entire content of the file and return the result as a string |
readline() |
Read the content of the first line of the file and return the result as a string |
readlines() |
Read the entire content of the file and return the result in a list (can output specified lines) |
2. Examples
(1), the original txt file
(2), read() function
file = "文本文档.txt"
# 打开文件
with open(file, "r", encoding='utf-8') as f:
# read():读取文件全部内容,以字符串形式返回结果
data = f.read()
print(data)
Run the screenshot:
(3), readline () function
file = "文本文档.txt"
# 打开文件
with open(file, "r", encoding='utf-8') as f:
# readline():读取文件第一行的内容,以字符串形式返回结果
data = f.readline()
print(data)
Run the screenshot:
(4), readline () function
file = "文本文档.txt"
# 打开文件
with open(file, "r", encoding='utf-8') as f:
# readlines():读取文件全部内容,以列表形式返回结果
data = f.readlines()
print("----------列表形式----------")
print(data)
print()
print("----------逐行形式----------")
for item in data:
print(item)
Run the screenshot:
3. Write files
1. Functions used
function name |
meaning |
write() |
Write the content to the file, no newline by default |
2. Examples
(1), the original txt file
(2), write () function
① Write in 'w' mode
file = "文本文档.txt"
# 打开文件
with open(file, "w", encoding='utf-8') as f:
# write():将内容写入文件,默认不换行
text = "世界之大,无奇不有!"
f.write(text)
Run the screenshot:
② Write in 'a' mode
file = "文本文档.txt"
# 打开文件
with open(file, "a", encoding='utf-8') as f:
# write():将内容写入文件,默认不换行
text = "\n世界之大,无奇不有!"
f.write(text)
Run the screenshot:
4. Another form of writing
Note: The above uses the writing form of with open, and the following methods can also be used to read and write txt files
1. Read files
file = "文本文档.txt"
# 打开文件
f = open(file, 'r', encoding='utf-8')
data = f.read()
print(data)
# 关闭文件
f.close()
Run the screenshot:
2. Write files
file = "文本文档.txt"
# 打开文件
f = open(file, 'a', encoding='utf-8')
text = "\n冰冻三尺,非一日之寒!"
f.write(text)
# 关闭文件
f.close()
Run the screenshot: