tgp1994 0 Report post Posted October 7, 2010 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
yordan 10 Report post Posted October 7, 2010 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
tgp1994 0 Report post Posted October 7, 2010 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
iGuest 3 Report post Posted November 8, 2010 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
tgp1994 0 Report post Posted November 8, 2010 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
iGuest 3 Report post Posted November 9, 2010 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