在 Python 中 gzip 文件

2022-01-18 00:00:00 python subprocess gzip

问题描述

我想用 Python 压缩一个文件.我正在尝试使用 subprocss.check_call(),但它一直失败并出现错误OSError:[Errno 2] No such file or directory".我在这里尝试的有问题吗?有没有比使用 subprocess.check_call 更好的压缩文件的方法?

I want to gzip a file in Python. I am trying to use the subprocss.check_call(), but it keeps failing with the error 'OSError: [Errno 2] No such file or directory'. Is there a problem with what I am trying here? Is there a better way to gzip a file than using subprocess.check_call?

from subprocess import check_call

def gZipFile(fullFilePath)
    check_call('gzip ' + fullFilePath)

谢谢!!


解决方案

试试这个:

check_call(['gzip', fullFilePath])

根据您对这些文件的数据所做的处理,Skirmantas 的链接到 http://docs.python.org/library/gzip.html 也可能有帮助.请注意页面底部附近的示例.如果您不需要访问数据,或者您的 Python 代码中没有数据,执行 gzip 可能是最简洁的方法,因此您不必在 Python 中处理数据.

Depending on what you're doing with the data of these files, Skirmantas's link to http://docs.python.org/library/gzip.html may also be helpful. Note the examples near the bottom of the page. If you aren't needing to access the data, or don't have the data already in your Python code, executing gzip may be the cleanest way to do it so you don't have to handle the data in Python.

相关文章