I have created a python script to read from a continuously updated file ('out.txt') and write to a different file ('received.txt') every 10 seconds. Now I need to figure out how to delete the already read data from the 'out.txt' file. Here is the code I have so far.
#!/usr/bin/python
import sys
import time
num_lines = sum(1 for line in open('out.txt')) #find the last line
print num_lines
sys.stdout = open('received.txt', 'w') #write to the received.txt file
print
f = open('out.txt', 'r') #open ‘out.txt’ with read permissions
f.readline(num_lines) #read the last line of ’out.txt’
while True: #start loop to print remaining lines in out.txt
for line in f:
print line
time.sleep(10) #sleep for 10 seconds
Do I delete the data in 'out.txt' after the loop or inside the loop? Should I use f.write for this? I am using Raspbian on a Raspberry Pi for this. The data for 'out.txt' looks like
iBeacon scan ...
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
3F234454-CF6D-4A0F-ADF2-F4911BA9FFA6 1 1 -71 -66
... keeps updating.
Any advice would be extremely helpful. Thank you!