python – 记录并压缩subprocess.call的输出

我想将subprocess.call(…)的输出重定向到xz或bzip2压缩文件.

我试过了 :

with lzma.open(log_path, "x") as log_file:
    subprocess.call(command, stdout=log_file, stderr=log_file)

但生成的文件不是有效的XZ压缩文件:

$xzcat logfile.xz
xzcat : logfile.xz: Format de fichier inconnu

(用法语表示“未知文件格式”).

当我只使用cat时,文件显示正确,最后有一些奇怪的数据(在脚本中启动的命令是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似乎是一个半有效的XZ存档文件,填充了未压缩的数据.我究竟做错了什么 ?

PS:当我做那样的事情时它会起作用:

output = subprocess.check_output(command)
log_file.write(output)

…但鉴于该命令需要很长时间(它是一个备份脚本),我希望能够在结束前看到日志(使用xzcat),以了解rsync正在做什么.

最佳答案 在子项被执行之前,重定向发生在文件描述符级别:之后没有运行父代码(与子代的stdout / stderr相关)(不运行来自lzma模块的Python代码).

要动态压缩以便在子进程仍在运行时可以看到输出,您可以将其输出重定向到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().

点赞