1

So I am writing a for loop program which reads line from the file.

file lines looks like this:

Program: Python User: Cma Code: 1234

The program goes like this:

while True:
 with open('file.txt', 'r') as fp:
    for i in fp:
        data = i.split()
        program = data[1]
        user = data[3]
        code = data[5]
        total = program + user + code
        print(total)
        file.seek(-len(i),1)

    else:
        print("Program Put to sleep!")
        time.sleep(5)

I believe my issue in this code is a logical error. The program is suppose to keep running and check of the file constantly. If there are 3 lines in the file it should read each one of them then unread it and when it finds there is no more lines in the file, the program goes to sleep and and start the loop from the new line entry.

In the program I have coded it keeps on reading from the beginning. Had a look at some examples from this platform but wasn't helpful so thought to ask. cheers

cma1941
  • 11
  • 2

1 Answers1

0

can you try deleting file.seek and changing with statement like this:

for line in fp.readlines():
    data = line.split(" ")
Ali Yılmaz
  • 1,657
  • 1
  • 11
  • 28
  • I have tried this method doesn't fulfill the requirement of the program. The coding which I have done does half of the job but it doesn't stop reading the read lines. Once the for loop, loops back it should look for new line entered in the file otherwise the program should go back to sleep. – cma1941 May 12 '18 at 15:33
  • @cma1941 oh well, i got it wrong then. that does not help, sorry. – Ali Yılmaz May 12 '18 at 15:40