Search code examples
pythonfileioreadlines

Incorrect output while reading text file in Python


I need to read the first 30 lines of a file.

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

300

am I missing something?


Solution

  • According to https://docs.python.org/2/library/stdtypes.html#file.readlines, the 30 is not the number of lines to read. It is a buffer hint given in bytes.