Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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.

Related questions

0 votes
1 answer
asked Jul 2, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 18, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 3, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Jul 9, 2019 in Java by Aditya98 (1.3k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers

501 comments

104k users

Browse Categories

...