代码点读机 / Python / readline

readline Python · 文件方法

每次只读文件的一行。

怎么用

文件.readline()

小例子

with open('a.txt', 'w', encoding='utf-8') as f:
    f.write('a\nb\n')
with open('a.txt', 'r', encoding='utf-8') as f:
    print(repr(f.readline()))

运行结果

'a\n'

提醒:结果包含末尾的换行符。

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

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