【问题标题】:Python: How to convert .7z to .rar or .zip?Python:如何将 .7z 转换为 .rar 或 .zip?
【发布时间】:2017-06-29 22:41:29
【问题描述】:

如何使用 Python 将 .7z 文件转换为 .rar 或 .zip 文件?

【问题讨论】:

    标签: python zip 7zip rar


    【解决方案1】:

    您可以分两步完成此操作。首先,解压 .7z 文件,然后将内容压缩为 zip 文件。

    解压 .7z 文件

    from lib7zip import Archive, formats
    
    with Archive('filename.7z') as archive:
        # extract all items to the directory
        # directory will be created if it doesn't exist
        archive.extract('directory')
    

    参考:https://github.com/harvimt/pylib7zip

    压缩成 zip 文件

    #!/usr/bin/env python
    import os
    import zipfile
    
    def zipdir(path, ziph):
        # ziph is zipfile handle
        for root, dirs, files in os.walk(path):
            for file in files:
                ziph.write(os.path.join(root, file))
    
    if __name__ == '__main__':
        zipf = zipfile.ZipFile('file.zip', 'w', zipfile.ZIP_DEFLATED)
        zipdir('tmp/', zipf)
        zipf.close()
    

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 1970-01-01
      • 2020-04-14
      • 2011-02-21
      • 2017-05-02
      • 2020-09-09
      • 2017-01-08
      • 2018-11-07
      • 2019-01-20
      相关资源
      最近更新 更多