Example for creating a compressed file using Python

by

This example shows that the same methods can be used to writing a normal text file and a compressed file. Corresponding reading functions are provided, too.

A mechanism to be able to read compressed and uncompressed files with the same function is shown in the function read_compressed_file. The function starts reading a compressed file. This will fail in the case that a plain text file is processed. The thrown error exception is catched and the function for reading an uncompressed file is invoked. So it is possible to use the same function for both types of files.


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os, gzip, traceback

WRITE_STRING = 'This is the content of the file.\n'

def write_file(filename):
    """ normal write to a file """
    try:
        fd = open(filename, 'wb')
        fd.write(WRITE_STRING)
    except (IOError, os.error), why:
        print 'Failed to write the file', filename, '\n Exception:', why
    finally:
        if fd is not None:
            fd.close()           

def write_compressed_file(filename):
    """ normal write to a file - but the result will be compressed """
    try:
        fd = gzip.GzipFile(filename=filename, mode='wb', compresslevel=9)
        fd.write(WRITE_STRING)
    except (IOError, os.error), why:
        print 'Failed to write the file', filename, '\n Exception:', why
    finally:
        if fd is not None:
            fd.close()           

def read_file(filename):
    """ reads the file """
    try:
        fd = open(filename, 'rb')
        return fd.read()
    except (IOError, os.error), why:
        print 'Failed to read the file', filename, '\n Exception:', why
    finally:
        if fd is not None:
            fd.close()           

def read_compressed_file(filename):
    """ reads the compressed file - if the file is not compressed read_file is called. """
    try:
        fd = gzip.GzipFile(filename=filename, mode='rb')
        fd.read(1) # trigger an exception if is not compressed
        fd.seek(0)
        print 'IS a compressed file'
        return fd.read()
    except (IOError, os.error), why:
        print 'not compressed file'
        return read_file(filename)
    finally:
        if fd is not None:
            fd.close()           

if __name__ == "__main__":
    from tempfile import NamedTemporaryFile
    file1 = NamedTemporaryFile().name
    print 'write to', file1

    print '\nwrite normal file - read normal file'
    write_file(file1)
    cmp = read_file(file1)
    if WRITE_STRING != cmp:
        print 'Strings are different:'
        print 'Written:', WRITE_STRING
        print 'Read:   ', cmp
    else:
        print 'normal ok.'

    print '\nwrite compressed file - read compressed file'
    write_compressed_file(file1)
    cmp = read_compressed_file(file1)
    if WRITE_STRING != cmp:
        print 'Strings are different:'
        print 'Written:', WRITE_STRING
        print 'Read:   ', cmp
    else:
        print 'compressed ok.'

    print '\nwrite normal file - read compressed file'
    write_file(file1)
    cmp = read_compressed_file(file1)
    if WRITE_STRING != cmp:
        print 'Strings are different:'
        print 'Written:', WRITE_STRING
        print 'Read:   ', cmp
    else:
        print 'read uncompressed with compressed function - ok.'

For further details on the used classes and functions these are some links into the Python Reference documentation:

I have tried to provide a pythonic example. If this is not the case please drop me a note. Thanks.

Technorati Tags: , ,

Advertisements

Tags: , , , ,

One Response to “Example for creating a compressed file using Python”

  1. Darny Says:

    Nice. Thanks!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: