Python - Reading from a text file that is being wr

2019-09-13 03:24发布

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.

1条回答
爷的心禁止访问
2楼-- · 2019-09-13 04:08

This might be caused by buffering in program 1. You can try flushing the output in program 1 after each write:

fid.write(str(i) + "\n")
fid.flush()

Another thing you can try is to run the Python interpreter in unbuffered mode for program 1. Use the python -u option.

Also, do you need to open the file for update (mode r+) in program 2? If you just want to read it, mode r is sufficient, or you can omit the mode when calling open().

查看更多
登录 后发表回答