Python 解压压缩包至文件夹、把整个文件夹打包至压缩包
Python 解压压缩包至文件夹、把整个文件夹打包至压缩包
1,Python 解压压缩包至文件夹
import zipfile
with zipfile.ZipFile(path_to_zip_file, ‘r’) as zip_ref:
zip_ref.extractall(directory_to_extract_to)
2,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),
os.path.relpath(os.path.join(root, file),
os.path.join(path, ‘..’)))
zipf = zipfile.ZipFile(‘Python.zip’, ‘w’, zipfile.ZIP_DEFLATED)
zipdir(‘tmp/’, zipf)
zipf.close()