Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flush the file write buffer in running python process with no file.close() statement?

I have a file that is being written to in a background Python 3.5 process. The file was opened in this process (which is basically a running Python script) but I forgot to include a file close statement to flush out the buffer occasionally in that script. The script is infinite unless it is ended manually and I (now) know that killing the python process will cause all the data in the buffer to be lost. Is there anyway to recover the data to be written in this already running process?

like image 582
Omnomnious Avatar asked Sep 07 '25 20:09

Omnomnious


1 Answers

As per the docs

first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.

Here is an example,

import os

with open("filename", "w") as f:
    while True:  # infinite program
        f.write("the text")
        f.flush()
        os.fsync(f.fileno())
like image 152
Shadow Avatar answered Sep 09 '25 19:09

Shadow