Why the second time I run “readlines” on the same file nothing is returned?

You need to seek to the beginning of the file. Use f.seek(0) to return to the begining:

>>> f = open('/tmp/version.txt', 'r')
>>> f
<open file '/tmp/version.txt', mode 'r' at 0xb788e2e0>
>>> f.readlines()
['2.3.4\n']
>>> f.seek(0)
>>> f.readlines()
['2.3.4\n']
>>>

Leave a Comment