0

Is it possible to write the next line with this code? How to do that? I want to read (n) line then do math to write (n+1), with unlimited looping.

import time
def follow(thefile):
    thefile.seek(0,2)
    while True:
        line = thefile.readline()
        if not line:
            time.sleep(0.1)
            continue
        yield line

if __name__ == '__main__':
    logfile = open("myfile.txt","r+")
    loglines = follow(logfile)
    for line in loglines:
        write.logfile ("5*2+1\n")
dease
  • 1
  • 1
  • @MauriceMeyer but it only works 1 time, I need looping non stop read line n and then write n + 1. – dease Aug 08 '21 at 14:48
  • Please share your code, otherwise nobody gets an idea what you are talking about – Maurice Meyer Aug 08 '21 at 14:59
  • @MauriceMeyer this is my new question, thanks https://stackoverflow.com/questions/68702899/how-to-reading-from-a-frequently-r-updated-file-with-python-code – dease Aug 08 '21 at 17:00

1 Answers1

0

You can achieve this by using methods of file objects.

Here is the link: https://docs.python.org/3/tutorial/inputoutput.html#methods-of-file-objects

You can use seek, write and read to achieve what you want.

kup
  • 731
  • 5
  • 18