So I have a python script (let's call it file_1.py
) that overwrites the content of a text file with new content, and it works just fine. I have another python script (file_2.py
) that reads the file and performs actions on the data in the file. With file_2.py
I've been trying to get when the text file is edited by file_1.py
and then do some stuff with the new data as soon as it's added. I looked into the subprocess module but I couldn't really figure out how to use it across different files. Here's what I have so far:
file_1.py:
with open('text_file.txt','w') as f:
f.write(assemble(''.join(data))) # you can ignore what assemble does, this part already works.
file_2.py:
while True:
f = open('text_file.txt','r')
data = f.read()
function(data)
f.close()
I thought that since I close and reopen the file every loop, the data in the file would be updated. However, it appears I was wrong, as the data remains the same even though the file was updated.
So how can I go about doing this?
Are you always overwiting the data in the first file, with the same data?
I mean, instead of appending or actually changing the data over time?
I see it working here when I change
with open('text_file.txt','wt') as f:
to
with open('text_file.txt','at') as f:
and I append some data. 'w'
will overwrite and if data doesn't change you will see the same data over and over.
Edit:
Another possibility (as discussed in the comments to OP self-answer) is the need to use f.flush()
after writing to the file. Despite the buffers being written to disk automatically when closing a file (or leaving a with
block), that write can take a moment, and if the file is read again before that moment, the updates will not be there (yet). To remove that uncertainty call flush after updating, wich forces the disk write.
If you sleep your reading code for enough time between readings (that is the reads are slow enough), the manual flush might not be needed. But if in doubt, or to do it the simple way and be sure, just use flush()
.
Okay, so it looks like I've solved my problem. According to this website, it says:
Python automatically flushes the files when closing them. But you may want to flush the data before closing any file.
Since the "automatic flushing" thing wasn't working, I tried to manually flush the I/O using file.flush()
, and it worked. I call that function every time right write to the file in file_1.py
.
EDIT: It seems that when time.sleep()
is called between readings of the file, it interferes and you have to manually flush the buffer.