-4
如何使用Python将.7z文件转换为.rar或.zip文件?Python:How to convert .7z to .rar or .zip?
如何使用Python将.7z文件转换为.rar或.zip文件?Python:How to convert .7z to .rar or .zip?
您可以分两步进行。首先,解压缩.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
压缩到压缩文件
#!/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()