[gzip用Python压缩文件

问题描述 投票:35回答:7

我想用Python将文件gzip压缩。我正在尝试使用subprocss.check_call(),但它始终失败,并显示错误“ OSError:[Errno 2] No such file or directory”。我在这里尝试的内容有问题吗?是否有比使用subprocess.check_call更好的gzip文件压缩方法?

from subprocess import check_call

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

谢谢!

python gzip subprocess
7个回答
18
投票

尝试一下:

check_call(['gzip', fullFilePath])

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


65
投票

有一个模块gzip。用法:

如何创建压缩的GZIP文件的示例:

import gzip
content = b"Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()

如何使用GZIP压缩现有文件的示例:

import gzip
f_in = open('/home/joe/file.txt')
f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()

编辑:

Jace Browning's answer在Python> = 2.7中使用with显然更简洁易读,因此我的第二个代码段将(并且应该)如下所示:

import gzip
with open('/home/joe/file.txt', 'rb') as f_in, gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
    f_out.writelines(f_in)

32
投票

以二进制(rb)模式读取原始文件,然后使用gzip.open创建可以使用gzip.open像普通文件一样写入的gzip文件:

writelines

甚至更短,您可以在一行上合并writelines语句:

import gzip

with open("path/to/file", 'rb') as orig_file:
    with gzip.open("path/to/file.gz", 'wb') as zipped_file:
        zipped_file.writelines(orig_file)

4
投票

使用with模块:

with open('path/to/file') as src, gzip.open('path/to/file.gz', 'wb') as dst:        
    dst.writelines(src)

您的错误:gzip告诉您文件import gzip import os in_file = "somefile.data" in_data = open(in_file, "rb").read() out_gz = "foo.gz" gzf = gzip.open(out_gz, "wb") gzf.write(in_data) gzf.close() # If you want to delete the original file after the gzip is done: os.unlink(in_file) 不存在。如果您仍然需要这样做,请确保文件在系统上存在并且使用的是绝对路径而不是相对路径。


4
投票

有关此文档的内容实际上非常简单

如何读取压缩文件的示例:

OSError: [Errno 2] No such file or directory'

如何创建压缩的GZIP文件的示例:

fullFilePath

如何使用GZIP压缩现有文件的示例:

import gzip
f = gzip.open('file.txt.gz', 'rb')
file_content = f.read()
f.close()

import gzip content = "Lots of content here" f = gzip.open('file.txt.gz', 'wb') f.write(content) f.close()

这就是整个文档。 。 。


3
投票

来自import gzip f_in = open('file.txt', 'rb') f_out = gzip.open('file.txt.gz', 'wb') f_out.writelines(f_in) f_out.close() f_in.close()

Gzip现有文件

https://docs.python.org/2/library/gzip.html

创建新的gzip文件:

docs for Python3

请注意,内容已转换为字节的事实

如果您不像上面的示例那样将内容创建为字符串/字节文字,则为另一种方法

import gzip
import shutil
with open('file.txt', 'rb') as f_in:
    with gzip.open('file.txt.gz', 'wb') as f_out:
        shutil.copyfileobj(f_in, f_out)

# or because I hate nested with statements

import gzip
import shutil
from contextlib import ExitStack
with ExitStack() as stack:
    f_in = stack.enter_context(open('file.txt', 'rb'))
    f_out = stack.enter_context(gzip.open('file.txt.gz', 'wb'))
    shutil.copyfileobj(f_in, f_out)

请参见import gzip content = b"Lots of content here" with gzip.open("file.txt.gz", "wb") as f: f.write(content) 以了解其他编码方法。


2
投票
import gzip
# get content as a string from somewhere else in the code
with gzip.open("file.txt.gz", "wb") as f:
    f.write(content.encode("utf-8"))
© www.soinside.com 2019 - 2024. All rights reserved.