I am writing a large python script with various subprocess.call to execute commands available in the system, and I have an issue because the output differs if it is printed into the terminal or if is redirected to a file.
For reproduce the problem, this is a small part of the script:
#!/usr/bin/env python # -*- coding: utf-8 -*- from subprocess import call print "Hello at first" call(["rsync", "-aHvz", "root@server1:/tmp/a", '.']) print "Hello at the end" Executing it from terminal return that in the correct order, print + rsync + print:
$ python keepconf.py Hello at the first receiving incremental file list sent 12 bytes received 123 bytes 270.00 bytes/sec total size is 55858143 speedup is 413764.02 Hello at the end Executing the same, but redirecting the output to a file:
$ python keepconf.py > /tmp/output $ cat /tmp/output receiving incremental file list sent 12 bytes received 123 bytes 270.00 bytes/sec total size is 55858143 speedup is 413764.02 Hello at the first Hello at the end The order now is rsync + print + print. Why is this behaviour?