When trying to write the stdout from a Python script to a text file (python script.py > log
), the text file is created when the command is started, but the actual content isn't written until the Python script finishes. For example:
script.py:
import time
for i in range(10):
print('bla')
time.sleep(5)
prints to stdout every 5 seconds when called with python script.py
, but when I call python script.py > log
, the size of the log file stays zero until the script finishes. Is it possible to directly write to the log file, such that you can follow the progress of the script (e.g. using tail
)?
EDIT It turns out that python -u script.py
does the trick, I didn't know about the buffering of stdout.