Ouput file redirection in Python

I am writing a backup script that I am going to execute in cronjob every night.

The script sets sys.stdout and sys.stderr to the output file to keep an event log.

For backup I use the following code

cmd = 'rsync -av --del --stats --filter "- .thumbnails/" ' + \ '--filter "- *~" --filter "- *.iso" --filter "- lost+found/" ' + \ '--filter "- .cache/" --filter "- tmp/" --filter "- *.mp3" ' + \ '--filter "- *.log" ' + srcDir + ' ' + dstDir print "Executing '"+cmd+"' ..." try: sys.stdout.flush() sys.stderr.flush() retcode = subprocess.call( cmd, stdin = sys.stdin, stdout = sys.stdout, stderr=sys.stderr, shell=False ) if retcode < 0: print >>sys.stderr, "Command was terminated by signal", -retcode elif retcode > 0: print >>sys.stderr, "Command returned code ", retcode except OSError, e: print >>sys.stderr, "Execution failed:", e 

I add print instructions before and after the subprocess call. The problem is that I get the subprocess call output before any output of my print instructions before the call. I have added flush () calls, but this has no effect.

Why is this happening and how can I change this behavior?

+6
python file
source share
3 answers

I found a solution here in Stackoverflow's answer.

Replace

 sys.stderr = sys.stdout = logFile = open( tmpLogFileName, 'a' ) 

from

 sys.stderr = sys.stdout = logFile = open( tmpLogFileName, 'a', 0 ) 

This suggests that python does not assign an output buffer to the file.

+3
source share

Have you tried to put flush calls outside the try block?

0
source share

Why are you printing on stderr? If the subprocess writes stdout while writing to stderr, this may explain the odd rotation.

0
source share

All Articles