14

I am writing a program in which I would like to be able to view a log file before the program is complete. I have noticed that, in python (2.7 and 3), that file.write() does not save the file, file.close() does. I don't want to create a million little log files with unique names but I would like to be able to view the updated log file before the program is finished. How can I do this?

Now, to be clear I am scripting using Ansys Workbench (trying to batch some CFX runs). Here's a link to a tutorial that shows what I'm talking about. They appear to have wrapped python, and by running the script I can send commands to the various modules. When the script is running there is no console onscreen and it appears to be eating all of the print statements, so the only way I can report what's happening is via a file. Also, I don't want to bring a console window up because eventually I will just run the program in batch mode (no interface). But the simulations take a long time to run and I can't wait for the program to finish before checking on what's happening.

NauticalMile
  • 1,625
  • 3
  • 16
  • 29
  • 2
    I found open(filename, mode, buffering =0) to be more suitable. Here, if you use 0 for 3rd positional argument, it will flush out the buffer immediately. – Osman Mamun Dec 01 '17 at 02:48

3 Answers3

37

You would need this:

file.flush()
# typically the above line would do. however this is used to ensure that the file is written
os.fsync(file.fileno())

Check this: http://docs.python.org/2/library/stdtypes.html#file.flush

file.flush() Flush the internal buffer, like stdio‘s fflush(). This may be a no-op on some file-like objects. Note flush() does not necessarily write the file’s data to disk. Use flush() followed by os.fsync() to ensure this behavior.

EDITED: See this question for detailed explanations: what exactly the python's file.flush() is doing?

enigmaticPhysicist
  • 1,518
  • 16
  • 21
starrify
  • 14,307
  • 5
  • 33
  • 50
5

Does file.flush() after each write help?

Hannu

Hannu
  • 11,685
  • 4
  • 35
  • 51
2

This will write the file to disk immediately:

file.flush()
os.fsync(file.fileno())

According to the documentation https://docs.python.org/2/library/os.html#os.fsync

Force write of file with filedescriptor fd to disk. On Unix, this calls the native fsync() function; on Windows, the MS _commit() function.

If you’re starting with a Python file object f, first do f.flush(), and then do os.fsync(f.fileno()), to ensure that all internal buffers associated with f are written to disk.

Community
  • 1
  • 1
Bert Wijnants
  • 385
  • 2
  • 5