-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()