How can unrar a file with python

前端 未结 5 1218
一生所求
一生所求 2020-11-30 10:48

I can to unzip a file if file is a .zip and unrar file if my file type is .rar. How i can do this work with python 2.7?

相关标签:
5条回答
  • 2020-11-30 11:37

    Try the pyunpack package:

    from pyunpack import Archive
    Archive('a.zip').extractall('/path/to')
    
    0 讨论(0)
  • 2020-11-30 11:38

    A good package for it is rarfile :

    Infos and docs here :

    https://pypi.python.org/pypi/rarfile/

    https://rarfile.readthedocs.org/en/latest/api.html

    0 讨论(0)
  • 2020-11-30 11:40

    After some deep diving, here are my findings:

    • RAR is not an free open format and is owned by RARLabs. You must install their DLL or exe first to work with RAR. Some programs like 7zip might already include this with them.
    • patool is application that provides uniform command line as wrapper to other external compression applications. Natively, it can only deal with TAR, ZIP, BZIP2 and GZIP without needing external support.
    • pyunpack is Python library that can only deal with zip natively but provides interface to patool.

    With this in mind, following things worked for me:

    • Make sure 7zip is installed
    • pip install patool pyunpack

    Then to use it,

    import pyunpack
    
    pyunpack.Archive(archive_file).extractall(extract_dir)
    
    0 讨论(0)
  • 2020-11-30 11:44

    Late, but I wasn't satisfied with any of the answers.

    pip install patool
    import patoolib
    patoolib.extract_archive("foo_bar.rar", outdir="path here")
    

    Works on Windows and linux without any other libraries needed.

    0 讨论(0)
  • 2020-11-30 11:45
    • How to unzip a file with Python 2.4?
    • https://pypi.python.org/pypi/rarfile/
    0 讨论(0)
提交回复
热议问题