how to print lines in a file by seeking a position from another text file in python?
<p>This is my code for getting lines from a file by seeking a position using f.seek() method but I am getting a wrong output. it is printing  from middle of the first line.
can u help me to solve this please?</p>

<pre><code>f=open(r"sample_text_file","r")
last_pos=int(f.read())
f1=open(r"C:\Users\ddadi\Documents\project2\check\log_file3.log","r")
f1.seek(last_pos)
for i in f1:
    print i
    last_position=f1.tell()
    with open('sample_text.txt', 'a') as handle:
        handle.write(str(last_position))
</code></pre>

<p>sample_text file contains the file pointer offset  which is returned by f1.tell() </p>

<p>If it's printing from the middle of a line that's almost certainly because your offset is wrong. You don't explain how you came by the magic number you use as an argument to <code>seek</code>, and without that information it's difficult to help more precisely.</p>

<p>One thing is, however, rather important. It's not a very good idea to use <code>seek</code> on a file that is open in text mode (the default in Python). Try using <code>open(..., 'rb')</code> and see if the process becomes a little more predictable. It sounds as though you may have got the offset by counting characters after reading in text mode, but good old Windows includes carriage return characters in text files, which are removed by the Python I/O routines before your program sees the text.</p>

python file