I am a beginner learning Python, and I just finished file operations recently. The specially shared
article is divided into two parts, the first part is the file reading type and the way of reading, the second part is the practice questions
File reading type
There are several ways to read files: read-only, write-only, append, read-write, write-read
Read only r
f = open('test',mode='r',encoding='utf-8') # 打开文件,读取方式为`r`,编码为UTF-8
f1 = f.read() # 读取文件
print(f1) #打印文件
f.close() # 关闭文件
In the read-only type, the file cannot be modified
Read and write r+
f = open('test',mode='rb',encoding='utf-8') # 读取方式变为`r+`
file = f.read()
f1 = f.read()
f.close()
When the read mode is r+
, the file can be written, but the print is read before writing
Read binaryrb
code is omitted, the file is read in binary mode.
I will show the rest in the form of a table, you can refer to the above code
Reading method | supplement |
---|---|
r | Read only, cannot be modified |
r+ | Read and write, the cursor is in front, modify from the first position, print out the modified number of characters |
rb | In a byte way to read |
w | Write only, if there is no target file to be written, it will be created, if there is, it will be cleared and then written |
wb | Convert to byte type write |
a | Open the file, move the cursor to the back of the text, and then append |
from | byet Join by type |
注
: r+
There are two manifestations, one is 读写
and the other is 写读
. requires attention. Also, w+
and a+
I did not write, because the learning phase with relatively few
Read function
I will show it in a table first, and then explain it in detail
Reading method | for occasion
Reading method | For occasion |
---|---|
read() | Read in characters, add parameters ( I ), read the previous i character |
seek() | Adjust cursor position |
tell() | Adjust the cursor position, need to be placed in seek() front |
readline() | Read line by line |
readlines() | Read each line as an element in the list, with line breaks \n |
truncate() | Intercept a paragraph and read it from back to front |
For example, there is a test
file named with the following contents:
456926667
This is a string of 10 characters
f = open('test',mode='r',encoding='utf-8') # 打开文件,读取方式为`r`,编码为UTF-8
f1 = f.read(5) # 读取文件中前5个字符
print(f1) #打印文件
f.close() # 关闭文件
The print result is
45692
f = open('test',mode='w',encoding='utf-8') # 打开文件,读取方式为`w`,编码为UTF-8
f1 = f.seek()
print(f1) #打印第五个字符
f.close() # 关闭文件
The print result is
2
I will not demonstrate the rest one by one, you can try it yourself if necessary
File reading method
f = open('test',mode='w',encoding='utf-8')
This file reading method can only read one file, and there are many codes
with open('test',mode='w',encoding='utf-8') as f:
pass
This file reading method can read multiple files at the same time, and the amount of code is relatively small
When two or more files need to be operated at the same time, the with open
method will be relatively simpler
Today’s file notes are temporarily shared here. If you are just learning together, you can add my group 456926667 to communicate and learn together. If you cannot share the group, please let me know about the reviewer's information.
There are exercises in the next article