Read the contents of the file: 1. Obtain character file objects 2. Do read function need to specify read by the open function

# 第一步 通过open函数获取文件对象
f=open("README.txt","mode="r",encoding="UTF-8")
# 第二步 通过read方法 读取字符
content=f.read(20)
# 第三步 输出读取到的内容
print(content)
# 注意 当多次读取的时候 每次都是从上一次读取到的位置开始读
content2=f.read(6)
print(content2)
# 第四步 释放资源
f.close()
Published 16 original articles · won praise 0 · Views 125

Guess you like

Origin blog.csdn.net/beyongboy/article/details/104878926