Jump to content
xisto Community
tgp1994

Simply Bzip2'ing A File? Should be simple, but not quite so :)

Recommended Posts

It seems that bzip2'ing a file via python isn't quite as simple as I thought it would be.I've looked up the reference for its library in the Python manual, and there doesn't seem to be any tutorials for simply bzipping a file. Would it be better to just call the bzip binary?

Share this post


Link to post
Share on other sites

For portability's sake it would be safer to find what's wrong in your way using the python library.If it's a one-shot, "quick-and-dirty" thing, you can try using the bzip binary, provided that it's your own server and you know where the binaries are.

Share this post


Link to post
Share on other sites

For portability's sake it would be safer to find what's wrong in your way using the python library.If it's a one-shot, "quick-and-dirty" thing, you can try using the bzip binary, provided that it's your own server and you know where the binaries are.


Ya, I 'spose executing the binary would be the easiest method in my case, thank you.

Share this post


Link to post
Share on other sites

To simply bzip a file with python

#!/usr/bin/env python# -*- coding: utf-8 -*-import bz2FILE = 'file_to_bzip'bz2_file = bz2.BZ2File('%s.bz2' % FILE, 'wb')data = open(FILE, 'rb').read()bz2_file.write(data)del(data)bz2_file.close()print '%s has been compressed and saved to current working directory.' % FILE

This is just simple example to just demonstrate how to bzip a file. Because it's only simple, you as the programmer needs to work on improving it.

Cheers,

MC

Share this post


Link to post
Share on other sites

To simply bzip a file with python

#!/usr/bin/env python# -*- coding: utf-8 -*-import bz2FILE = 'file_to_bzip'bz2_file = bz2.BZ2File('%s.bz2' % FILE, 'wb')data = open(FILE, 'rb').read()bz2_file.write(data)del(data)bz2_file.close()print '%s has been compressed and saved to current working directory.' % FILE

This is just simple example to just demonstrate how to bzip a file. Because it's only simple, you as the programmer needs to work on improving it.

Cheers,

MC

I think I used a different technique awhile ago, (doesn't bz2 delete the uncompressed file once it's done?)

But thanks anyways :)

Share this post


Link to post
Share on other sites

The I/O features of bz2 does not have the ability to delete any file, it is only able to overwrite any file if permissions allow it to.The above program opens the file we want to compress in read-only mode, then reads the whole file into memory and then passes that information to bz2 to be compressed into it's bz2 file that has write mode. Reading the whole file into memory can later cause issues if you have very large files to compress. This is why you need to understand bz2's ability, as I believe it splits it's data up into 900KB chunks for it's compression, so you'll be better off streaming 900KB a time to it to be compressed (please check that up though).Cheers,MC

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.