我正在尝试在特定目录名称中“下载”所有zip文件,并将其所有内容提取到名为“ extracted”的目录中。
我不知道为什么,仅迭代现有文件名后,我得到一个错误,即没有这样的文件...
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'
答案 0 :(得分:0)
您将从一个目录中获取文件名列表,然后更改为另一个目录,并尝试从该目录中提取可能不存在的文件:
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:
您应该能够在找到它的目录中打开文件。