代码点读机 / Python / read

read Python · 文件方法

把文件内容一次性全部读出来。

怎么用

文件.read(大小=-1)

小例子

with open('a.txt', 'w', encoding='utf-8') as f:
    f.write('hello')
with open('a.txt', 'r', encoding='utf-8') as f:
    print(f.read())

运行结果

hello

提醒:大文件别一次性读。

官方文档:https://docs.python.org/zh-cn/3/library/io.html#io.TextIOBase

在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条