# 读取文件,open语句
f =open(path1,"r")print(type(f))print(f)print(f.read())print("读取完毕")# open("路径","模式","encoding = "编码") 如果有中文,需要添加编码格式utf8# 模式: r:读取文件,默认:w:写入,rw:读取+写入,a:追加# 简单的读取方法:.read()读取后,光标将会留在读取末尾print("二次读取")print(f.read())print("二次读取完毕")# 运行一次.read()之后,光标位于末尾,再次读取输出为空
f.seek(0)# 光标重新回到某个位置print("三次读取")print(f.read())print("三次读取完毕")
f.close()print("四次读取")print(f.read())# 关闭后无法读取<class'_io.TextIOWrapper'><_io.TextIOWrapper name='C:\\Users\\lizheying\\Desktop\\ziliao\\text.txt' mode='r' encoding='cp936'>
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
读取完毕
二次读取
二次读取完毕
三次读取
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
三次读取完毕
四次读取
--------------------------------------------------