gzip a file in Python

2019-02-04 02:54发布

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)

Thanks!!

6条回答
唯我独甜
2楼-- · 2019-02-04 03:24

Try this:

check_call(['gzip', fullFilePath])

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.

查看更多
淡お忘
3楼-- · 2019-02-04 03:32
import gzip

def gzip_file(src_path, dst_path):
    with open(src_path, 'rb') as src, gzip.open(dst_path, 'wb') as dst:
        for chunk in iter(lambda: src.read(4096), b""):
            dst.write(chunk)
查看更多
Luminary・发光体
4楼-- · 2019-02-04 03:33

Use the gzip module:

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)

Your error: OSError: [Errno 2] No such file or directory' is telling you that the file fullFilePath does not exist. If you still need to go that route, please make sure that file exists on your system and you are using an absolute path not relative.

查看更多
你好瞎i
5楼-- · 2019-02-04 03:35

There is a module gzip. Usage:

Example of how to create a compressed GZIP file:

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

Example of how to GZIP compress an existing file:

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()

EDIT:

Jace Browning's answer using with in Python >= 2.7 is obviously more terse and readable, so my second snippet would (and should) look like:

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)
查看更多
你好瞎i
6楼-- · 2019-02-04 03:37

the documentation on this is actually insanely straightforward

Example of how to read a compressed file:

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

Example of how to create a compressed GZIP file:

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

Example of how to GZIP compress an existing file:

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()

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

That's the whole documentation . . .

查看更多
迷人小祖宗
7楼-- · 2019-02-04 03:47

In Python 2.7 format:

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)

Even shorter (Tested on python 2.7.6)

with open('path/to/file') as src, gzip.open('path/to/file.gz', 'wb') as dst:        
    dst.writelines(src)
查看更多
登录 后发表回答