0

How can I make sure a file writing process has fully finished in a python script? In my case, a few lines later in the same script that same file might get read.

Will a with statement work in this case? An objection might be that allthough the with-statement may have made sure a file.close command is issued it might take some time for the OS to complete the command and in the mean time a read process on the file might have initiated.

Any thoughts?

TheCat
  • 137
  • 4
  • If you asking if closing file is a blocking operation, yet is it. – Łukasz Rogalski Jul 15 '16 at 12:55
  • @Rogalski: Okay, but the question really is can I make sure that the entire process of writing and closing has finished before attempting a read. – TheCat Jul 15 '16 at 13:00
  • Can you have a look at this answer if it addresses your case: http://stackoverflow.com/questions/9824806/how-come-a-file-doesnt-get-written-until-i-stop-the-program – Swadhikar Jul 15 '16 at 13:03
  • Closing a file flushes the buffer, so yes, you can safely start reading after a file has been closed (provided you have a new or pre-existing open file handle for the same file of course). Or you could just call file.flush(). – Martijn Pieters Jul 15 '16 at 13:10

0 Answers0