在 Python 中读取文本文件时输出不正确

Incorrect output while reading text file in Python

我需要读取文件的前 30 行。

with open(filename) as f:
    lines = f.readlines(30)
print len(lines)

300

我是不是漏了什么?

根据https://docs.python.org/2/library/stdtypes.html#file.readlines30不是要读取的行数。它是 bytes.

中给出的缓冲区提示

阅读评论后,我决定 "answer" 解决以下问题:如何从文件中读取前 30 行。答案是:readline()

lines = []
for i in range(30):
    lines.append(f.readline())

完成。