Reputation: 728
I have a python script that takes a long time to run.
I placed print-outs throughout the script to observe its progress. As this script different programs, some of whom print many messages, it is unfeasible to print directly to the screen.
Therefore, I am using a report file
f_report = open(os.path.join("//shared_directory/projects/work_area/", 'report.txt'), 'w')
To which I print my massages:
f_report.write(" "+current_image+"\n")
However, when I look at the file while the script is running, I do not see the messages. They appear only when the program finishes and closes the file, making my approach useless for monitoring on-going progress. What should I do in order to make python output the messages to the report file in real time?
Many thanks.
Upvotes: 3
Views: 4480
Reputation: 1102
try this:
newbuffer = 0
f_report = open(os.path.join("//shared_directory/projects/work_area/", 'report.txt'), 'w', newbuffer)
it sets up a 0 buffer which will push OS to write content to file "immediately". well, different OS may behavior differently but in general content will be flushed out right away.
Upvotes: 2
Reputation: 1769
You should use flush() function to write immediately to the file.
f_report.write(" "+current_image+"\n")
f_report.flush()
Upvotes: 6