Python - Reading from a text file that is being written in Windows

送分小仙女□ 提交于 2019-12-12 02:44:08

问题


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:


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().



来源:https://stackoverflow.com/questions/33098740/python-reading-from-a-text-file-that-is-being-written-in-windows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!