Proper
Proper

Reputation: 155

Python continuously write and save new data to file

I am trying to write data to file continuously to the end of file so that data becomes available for read as soon as it is written but it seems that changes do not get commuted without file being closed. I can open and close file every time script loops but I think its a bad idea and there must be a way to commit changes at the end of every loop

target = open("file.txt","a")

with open('test.raw',"rb") as f:
    byte = f.read(160)
    while byte != b"":
        byte = f.read(160)
        target_speech.write(data_value)   

How can I get these changes committed inside the loop?

Upvotes: 3

Views: 6753

Answers (1)

jez
jez

Reputation: 15349

Use target.flush() immediately after target.write(...)

Upvotes: 7

Related Questions