You can process the first time the whole file and use after the iteration of lines the method
tell
of of the file object, which tells you where you are (at which byte). You can convert the integer to a str
and write it to a file. Next time the script looks for this file and if the file is present, it should load the content of the file, convert it back to an int
and you use before you start iterating over the lines, you use seek(position)
on the file object. Then you have the position, where your script finished last time.In [20]: with open('birds.txt') as fd: ...: for line in fd: ...: print(line.strip()) ...: print(fd.tell()) ...: #fd.tell() <- file is already closed ...: 2010-01-01 01:01:00.0000 left 2010-11-01 01:01:00.0000 right 2010-10-01 01:01:00.0000 right 91So, if a program writes now to birds.txt, it starts as byte position 91.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
All humans together. We don't need politicians!