我希望从 .zip 中的 Python 中解压缩特定文件夹:
eg archive.zip
contains the folders foo
and bar
, I want to unzip foo
to a specific location, retaining it’s folder structure.
原文由 James 发布,翻译遵循 CC BY-SA 4.0 许可协议
我希望从 .zip 中的 Python 中解压缩特定文件夹:
eg archive.zip
contains the folders foo
and bar
, I want to unzip foo
to a specific location, retaining it’s folder structure.
原文由 James 发布,翻译遵循 CC BY-SA 4.0 许可协议
你应该拉上你的拉链….
import zipfile
archive = zipfile.ZipFile('archive.zip')
for file in archive.namelist():
if file.startswith('foo/'):
archive.extract(file, 'destination_path')
archive.close()
或者只是使用更安全的方法。随着将关闭你的拉链。
import zipfile
with zipfile.ZipFile('archive.zip') as archive:
for file in archive.namelist():
if file.startswith('foo/'):
archive.extract(file, 'destination_path')
原文由 Hildermes José Medeiros Filho 发布,翻译遵循 CC BY-SA 4.0 许可协议
2 回答5.1k 阅读✓ 已解决
2 回答1.1k 阅读✓ 已解决
4 回答1k 阅读✓ 已解决
3 回答1.1k 阅读✓ 已解决
3 回答1.2k 阅读✓ 已解决
1 回答1.7k 阅读✓ 已解决
1 回答1.2k 阅读✓ 已解决
检查
zipfile
模块。对于你的情况: