Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start iterating a file at specific line?

I’m iterating through a file’s lines with enumerate(), and sometimes would need to start the iterating at a specific file line, so I attempted testfile.seek(), e.g. if I want to start iterating the file again at line 10 then testfile.seek(10):

test_file.seek(10)

for i, line in enumerate(test_file):
    …

Yet the test_file always keep iterating starting at the very first line 0. What could I be doing wrong? Why isn’t the seek() working? Any better implementations would be appreciated as well.

Thank you in advance and will be sure to upvote/accept answer

like image 756
Jo Ko Avatar asked Oct 16 '25 12:10

Jo Ko


1 Answers

Ordinary files are sequences of characters, at the file system level and as far as Python is concerned; there's no low-level way to jump to a particular line. The seek() command counts the offset in bytes, not lines. (In principle, an explicit seek offset should only be used if the file was opened in binary mode. Seeking on a text file is "undefined behavior", since logical characters can take more than one byte.)

Your only option if you want to skip a number of lines is to read and discard them. Since iterating over a file object fetches it one line at a time, a compact way to get your code to work is with itertools.islice():

from itertools import islice

skipped = islice(test_file, 10, None)  # Skip 10 lines, i.e. start at index 10
for i, line in enumerate(skipped, 11):
    print(i, line, end="")
    ...
like image 169
alexis Avatar answered Oct 18 '25 01:10

alexis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!