Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Python by (12.7k points)

I just need to open a file and read each and every line by using f.seek() and f.tell():

test.txt

abc

def

ghi

jkl

Code:

f = open('test.txt', 'r')

last_pos = f.tell()  # get to know the current position in the file

last_pos = last_pos + 1

f.seek(last_pos)  # to change the current position in a file

text= f.readlines(last_pos)

print text

It actually reads the whole file

1 Answer

0 votes
by (26.4k points)

Kindly check the below code:

f = open( ... )

f.seek(last_pos)

line = f.readline()  # no 's' at the end of `readline()`

last_pos = f.tell()

f.close()

simply recall, last_pos isn't a line number in your document, it's a byte offset from the start of the file - there's no reason for incrementing/decrementing it.

Interested to learn python in detail? Come and Join the python course.

Browse Categories

...