How to get the specific number of lines in a file using python programming?
<p>This is my code for getting 20 lines every time but f1.tell() gives last position of the file. So i cannot get 20 lines for the next time.
Can anyone help me to do this? please</p>

<pre><code>f1=open("sample.txt","r")
last_pos=0
while true:
    f1.seek(last_pos)
    for line,i in enumerate(f1.readlines()):
        if line == 20:
            last_pos=f1.tell()
            break
        else:
            print i
sample.txt file contains below data
1
2
3
4
.
.
.
.
40
I want Output like
1
2
3
4
.
.
.
20
20
21
22
23
24
25
.
.
.
.
39
39
40
</code></pre>

<p>Jean-FranÃ§ois Fabre already explained that <code>readline</code> could read the whole file.</p>

<p>But anyway you should never mix line level input (<code>readline</code>) and byte level input (<code>tell</code>, <code>seek</code> or <code>read</code>). As the low level OS system calls can only read a byte count, <code>readline</code> actually reads a <em>buffer</em> and searches for a newline in it. But the file pointer given by <code>tell</code> is positionned at the end of the buffer and <strong>not</strong> at the end of the line.</p>

<p>There is no way to set the file pointer at the end of a line except by processing the file one char at a time and manually detecting the end of line.</p>

python file