It looks like python keeps track of each run of read() and readline(). It is incremental, by reach run, and in the end, it does not return any value. How to find this counter, and read a specific line at any time?
EDIT: My goal is to read a large file of a few Gb in size, hundreds of thousands of lines. If this an iterator then it is insufficient, I do not want to load the whole file in the memory. How do I jump to a specific line without having to read unnecessary lines?
A text file with just 3 lines.
# cat sample.txt
This is a sample text file. This is line 1
This is line 2
This is line 3
# python
Python 3.7.5 (default, Nov 7 2019, 10:50:52)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open('sample.txt', 'r')
>>> file.readline()
'This is a sample text file. This is line 1\n'
>>> file.readline()
'This is line 2\n'
>>> file.readline()
'This is line 3\n'
>>> file.readline()
''
>>> file.readline()
''
>>> file.read()
''
>>> file.read(0)
''
>>> file.read()
''
>>>
# python
Python 3.7.5 (default, Nov 7 2019, 10:50:52)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> file = open('sample.txt', 'r')
>>> file.read()
'This is a sample text file. This is line 1\nThis is line 2\nThis is line 3\n'
>>> file.read()
''
>>> file.readline()
''
>>>