【问题标题】:How to compress multiple files in linux using python script?如何使用python脚本在linux中压缩多个文件?
【发布时间】:2017-05-26 03:57:07
【问题描述】:

我想压缩与用户输入的模式匹配的所有日志,我正在使用以下代码但出现错误。

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

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

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

【问题讨论】:

    标签: regex linux python-3.x compression


    【解决方案1】:

    您在 python 中为gzip 提供字符串purato.log.2017-08*,但是当您在shell 中执行gzip purato.log.2017-08* 时,将调用在gzip 之前替换的通配符*

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

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

    【讨论】:

    • 我收到此错误 gzip:压缩数据未写入终端。使用 -f 强制压缩
    • 它必须是一个字符串才能让 shell 解释它:['gzip %s' % input]
    猜你喜欢
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多