

shutils is a module we generally don't associate with compression and decompression, but it provides utility methods for working with archives and can be a convenient way for producing tar, gztar, zip, bztar or xztar archives.This module uses the already mentioned zlib compression algorithm and serves as an interface similar to the gzip and gunzip utilities.


It can produce higher compression ratio than some older methods and is the algorithm behind the xz utility (more specifically LZMA2). lzma is both name of the algorithm and Python module.It also works only on individual files and therefore can't create archives. This algorithm is generally more effective than the deflate method, but might be slower. bz2 is a module that provides support for bzip2 compression.More about this library can be found on Wikipedia. So, by using this Python module, you're essentially using gzip compatible compression algorithm without the convenient wrapper. zlib is a library and Python module that provides code for working with Deflate compression and decompression format which is used by zip, gzip and many others.So, let’s first take a look at each of them and see why you might want to use them: All The FormatsĪs mentioned above, Python has library for (almost) every tool/format imaginable.
#Recompress files how to#
So, to help you navigate through all the available options, we will in this article explore all of these modules and learn how to compress, decompress, verify, test and secure our archives of all kinds of formats with help of Python's standard library. With all these options, deciding what might be the right tool for the task at hand might not be so obvious, though. Whether it’s basics like tar and zip, specific tools or formats such as gzip and bz2 or even more exotic formats like lzma, Python has it all. Python standard library provides great modules and tools for pretty much any task you can think of and modules for working with compressed files are no exception.
