我正在寻找从 Python 中的 .zip 解压缩特定文件夹:
例如archive.zip包含文件夹foo,bar我想解压缩foo到特定位置,保留它的文件夹结构。
检查zipfile模块。
对于您的情况:
import zipfile
archive = zipfile.ZipFile('archive.zip')
for file in archive.namelist():
if file.startswith('foo/'):
archive.extract(file, 'destination_path')
使用 zipfile 库非常非常慢。这是更好的方法:
os.system('unzip -P your-password path/to/file.zip')