I am using Windows 7, Python 2.7. I am trying to write to a text file with one file ID in one program that continues writing new data/numbers for several minutes.
In a separate program, after the writing has begun, I am trying to read from the file that is being written in order to update and plot the data in the file.
While the first program is writing the data, I am unable to read the data until it finishes. Here is some example code to illustrate my point:
Program 1:
import time
fid = open("test1.txt", "w+")
for i in range(0, 5):
fid.write(str(i) + "\n")
print(i)
time.sleep(5)
fid.close()
Program 2:
fid = open("test1.txt", "r+")
dataList = fid.read().splitlines()
print(dataList)
fid.close()
Executing Program 2 while Program 1 is running does not allow me to see any changes until Program 1 is completed.
Is there a way to fix this issue? I need to keep the reading and writing in two separate programs.