![](/img/trans.png)
[英]Extracting files with specific extensions from a lot of ZIP archives using Python
[英]Extracting zip files using python
我正在尝试将所有 zip 文件放在“已下载”的特定目录名称中,并将其所有内容提取到名为“已提取”的目录中。
我不知道为什么,在我只迭代现有文件名之后,我收到一个错误,即没有这样的文件......
allFilesList = os.listdir(os.getcwd()+"/downloaded")
print allFilesList #verify - correct expected list
from zipfile import ZipFile
os.chdir(os.getcwd()+"/extracted/")
print os.getcwd() #verify - correct expected dir
for fileName in allFilesList:
print fileName
with ZipFile(fileName, 'r') as zipFileObject:
if os.path.exists(fileName):
print "Skipping extracting " + fileName
continue
zipFileObject.extractall(pwd='hello')
print "Saving extracted file to extracted/",fileName
print "all files has been successfully extracted"
错误信息:
File "program.py", line 77, in <module>
with ZipFile(fileName, 'r') as zipFileObject:
File "/usr/lib/python2.7/zipfile.py", line 779, in __init__
self.fp = open(file, modeDict[mode])
IOError: [Errno 2] No such file or directory: 'zipFile1.zip'
您从一个目录获取文件名列表,然后更改为另一个目录,并尝试从该目录中提取可能不存在的文件:
allFilesList = os.listdir(os.getcwd()+"/downloaded")
# ...
os.chdir(os.getcwd()+"/extracted/")
# ...
with ZipFile(fileName, 'r') as zipFileObject:
如果您将该文件ZipFile
命令更改为以下内容:
with ZipFile(os.path.join("..", "downloaded", fileName), 'r') as zipFileObject:
您应该能够在找到它的目录中打开该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.