【发布时间】:2015-12-06 03:03:54
【问题描述】:
我想将 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在做什么。
【问题讨论】:
标签: python python-3.x compression subprocess