Write and compile the output of subprocess.call

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.

+4
1

, : ( stdout/stderr) ( Python lzma ).

" ", , , xz:

#!/usr/bin/env python3
import subprocess

with open('logfile.xz', 'xb', 0) as log_file:
    subprocess.call("command | xz -kezc -", shell=True,
                    stdout=log_file, stderr=subprocess.STDOUT)

: open(), lzma.open(): xz.


Python, python:

#!/usr/bin/env python3
import lzma
from subprocess import Popen, PIPE, STDOUT
from shutil import copyfileobj

with lzma.open('logfile.xz', 'xb') as log_file, \
     Popen('command', stdout=PIPE, stderr=STDOUT) as process:
    copyfileobj(process.stdout, log_file)

: lzma.open().

+2

Source: https://habr.com/ru/post/1618753/


All Articles