I want to redirect the output subprocess.call(...)to a compressed xz- or bzip2 file.
I tried:
with lzma.open(log_path, "x") as log_file:
subprocess.call(command, stdout=log_file, stderr=log_file)
but the resulting file is not a valid XZ-compressed file:
$ xzcat logfile.xz
xzcat : logfile.xz: Format de fichier inconnu
(which in French means "unknown file format").
When I use only cat, the file is displayed correctly, with some strange data at the end (there is a command running in the script rsync):
& cat logfile.xz
sending incremental file list
prog/testfile
sent 531.80K bytes received 2.71K bytes 1.07M bytes/sec
total size is 14.21G speedup is 26,588.26
7zXZ ִF D! }YZ
logfile.xz appears to be a semi-valid XZ archive archive filled with uncompressed data. What am I doing wrong?
PS: It works when I do something like this:
output = subprocess.check_output(command)
log_file.write(output)
... but given that the command takes a lot of time (this is a backup copy of the script), I want to see the log (s xzcat) to the end to find out what rsync does.