I already checked this answer but it didn't work.
I am looking for a way to force python to write a file (let's call it file_A.py
) before exiting the program. I tried the suggested approach in the above answer, but it only worked when the destination file existed and we add more content. In my situation, the file does not exist and it is supposed to be generated before the program exits normally. The reason is that the generated file has to be used in the following steps of the program.
What I've coded is as follows:
with open('file_A.py', 'w') as f:
f.write(content)
f.flush()
os.fsync(f.fileno())
But file_A.py
was not generated before the program finishes execution normally. How do I fix it?