Home >Backend Development >Python Tutorial >Python self-study file operations
python video tutorialColumn introduction to self-study file operations
Recommended (free ): python video tutorial
I am a beginner in learning Python and have just finished learning file operations recently. Specially shared
The article is divided into two parts. The first part is the file reading type and the reading method. The second part is the practice questions
File reading type
File reading operations include the following: 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() # 关闭文件
Under read-only type, the file cannot be modified
Read and writer
f = open('test',mode='rb',encoding='utf-8') # 读取方式变为`r+` file = f.read() f1 = f.read() f.close()
When the read mode isr
, the file can be written, but what is printed is the read before writing
Binary readingrb
The code is omitted , the file is read in binary mode.
I will present the rest in the form of a table. You can refer to and compare the above codes
Reading method | Supplementary |
---|---|
r | Read only, cannot be modified |
r | Read and write, With the cursor in front, start modifying from the first position and print out the modified number of characters |
Read in | byte mode
|
Write only, if the target file to be written does not exist, create it, otherwise clear it and write it again | |
Convert to | byte type and write
|
Open the file, move the cursor to the end of the text, and then proceed Append | |
Add |
|
Note:
r has two performances, one is
reading and writing, and the other is
writing and reading. requires attention. In addition, I did not write
w and
a because they are relatively rarely used in the learning stage
Reading function
I will present it in table form first, and then explain it in detailUsed for occasions | |
---|---|
Read in characters, you can add parameters ( | I), read the first i characters
|
Adjust the cursor position | |
Adjust the cursor position, it needs to be placed at | seek()before
|
read line by line | |
Read each line as an element in the list, with line breaks | \n
|
Intercept a section and read it out, Read from back to front |
. The content of the file is as follows:
This is a string of 10 characters long
f = open('test',mode='r',encoding='utf-8') # 打开文件,读取方式为`r`,编码为UTF-8 f1 = f.read(5) # 读取文件中前5个字符 print(f1) #打印文件 f.close() # 关闭文件
The print result is
456922f = open('test',mode='w',encoding='utf-8') # 打开文件,读取方式为`w`,编码为UTF-8 f1 = f.seek() print(f1) #打印第五个字符 f.close() # 关闭文件Print The result is
I will not demonstrate the rest one by one. If necessary, you can try it yourselfFile reading Fetching 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 simple
The above is the detailed content of Python self-study file operations. For more information, please follow other related articles on the PHP Chinese website!