How to use lzma2 in python code?
Question: I know there is a module called pylzma. But it only support lzma, not lzma2.

My current solution is using `subprocess.call()` to call 7z program.

Is there a better way?


Answer: You can use [backports.lzma](https://github.com/peterjc/backports.lzma), see
for more info: [Python 2.7: Compressing data with the XZ format using the
"lzma"
module](http://stackoverflow.com/questions/22370068/python-2-7-compressing-
data-with-the-xz-format-using-the-lzma-module)

Then it's simply a matter of doing e.g.:

    
    
    from backports import lzma
    
    with open('hello.xz', 'wb') as f:
        f.write(lzma.compress(b'hello', format=lzma.FORMAT_XZ))
    

Or simpler (XZ format is default):

    
    
    with lzma.open('hello.xz', 'wb') as f:
        f.write(b'hello')
    

See <http://docs.python.org/dev/library/lzma.html> for usage details.

