Home >Backend Development >Python Tutorial >Python self-study file operations

Python self-study file operations

coldplay.xixi
coldplay.xixiforward
2020-12-16 17:44:103347browse

python video tutorialColumn introduction to self-study file operations

Python 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

##rbRead in wWrite only, if the target file to be written does not exist, create it, otherwise clear it and write it againwb Convert to aOpen the file, move the cursor to the end of the text, and then proceed Append abAdd with type
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
byte mode
byte type and write
byet

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 detail

Reading methodUsed for occasionsread()Read in characters, you can add parameters (seek()Adjust the cursor positiontell()Adjust the cursor position, it needs to be placed at readline()read line by linereadlines()Read each line as an element in the list, with line breaks ##truncate()For example, there is a file named
I), read the first i characters
seek()before
\n
Intercept a section and read it out, Read from back to front
test

. The content of the file is as follows:

456926667

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

45692

f = open('test',mode='w',encoding='utf-8')  # 打开文件,读取方式为`w`,编码为UTF-8
f1 = f.seek()
print(f1) #打印第五个字符
f.close() # 关闭文件
Print The result is

2

I will not demonstrate the rest one by one. If necessary, you can try it yourself

File 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!

Statement:
This article is reproduced at:jianshu.com. If there is any infringement, please contact admin@php.cn delete