gzip a file in Python

后端 未结 7 2057
悲&欢浪女
悲&欢浪女 2020-12-28 12:47

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 ther

相关标签:
7条回答
  • 2020-12-28 12:51

    From the docs for Python3

    Gzip an existing file

    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)
    

    Create a new gzip file:

    import gzip
    content = b"Lots of content here"
    with gzip.open("file.txt.gz", "wb") as f:
        f.write(content)
    

    Note the fact that content is turned into bytes

    Another method for if you aren't creating content as a string/byte literal like the above example would be

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

    See this SO question for a discussion of other encoding methods.

    0 讨论(0)
  • 2020-12-28 12:52
    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)
    
    0 讨论(0)
  • 2020-12-28 12:53

    Read the original file in binary (rb) mode and then use gzip.open to create the gzip file that you can write to like a normal file using 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)
    

    Even shorter, you can combine the with statements on one line:

    with open('path/to/file', 'rb') as src, gzip.open('path/to/file.gz', 'wb') as dst:
        dst.writelines(src)
    
    0 讨论(0)
  • 2020-12-28 12:55

    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 . . .

    0 讨论(0)
  • 2020-12-28 13:03

    There is a module gzip. Usage:

    Example of how to create a compressed GZIP file:

    import gzip
    content = b"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)
    
    0 讨论(0)
  • 2020-12-28 13:09

    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.

    0 讨论(0)
提交回复
热议问题