[英]How to speed up zip file extraction in Python
我正在尝试从 Python 中的 zip 文件中提取数据,但速度有点慢。 谁能给我建议,看看我是否正在做一些明显使它变慢的事情?
def go_through_zip(zipname):
out = {}
with ZipFile(zipname) as z:
for filename in z.namelist():
with z.open(filename) as f:
try:
outdict = make_dict(f)
out.update(outdict)
except:
print("File is not in the correct format")
return out
make_dict(f) 只是获取文件路径并制作字典,这个函数可能也很慢,但这不是我现在想要加速的。
尝试使用以下代码提取文件。 只要提取的文件大小合理,它就可以快速运行。
# importing required modules
from zipfile import ZipFile
# specifying the zip file name
file_name = "my_python_files.zip"
# opening the zip file in READ mode
with ZipFile(file_name, 'r') as zip:
# printing all the contents of the zip file
zip.printdir()
# extracting all the files
print('Extracting all the files now...')
zip.extractall()
print('Done!')
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.