如何使用python脚本在linux中压缩多个文件?

时间:2017-05-26 03:57:07

标签: regex linux python-3.x compression

我想压缩所有与用户输入的模式匹配的日志,我使用下面的代码但是出错了。

import subprocess
input=input("Enter log details to compress")
subprocess.call(['gzip',input],shell=True)

输入为:purato.log.2017-08 *

错误:gzip:未写入终端的压缩数据。使用-f强制压缩。

1 个答案:

答案 0 :(得分:0)

你在python中给gzip字符串purato.log.2017-08*,但是当你在shell中执行gzip purato.log.2017-08*时,在*之前替换了通配符gzip被调用。

要复制此行为,请在shell功能上启用call()

subprocess.call(['gzip purato.log.2017-08*'],shell=True)