gpt4 book ai didi

python - 未绑定(bind)本地错误 : local variable 'fullfilename' referenced before assignment

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:50 25 4
gpt4 key购买 nike

我正面临一个错误:

UnboundLocalError: local variable 'fullfilename' referenced before assignment

代码块:

caminho_path = self.tempDir
arquivos = os.listdir(self.tempDir)

for arquivo in arquivos:
if arquivo.endswith(".zip"):
fullfilename = os.path.join(caminho_path, arquivo)

self.driver.implicitly_wait(5)

sleep(10)

with ZipFile(fullfilename, 'r') as zipObj:
listOfFileNames = zipObj.namelist()
for fileName in listOfFileNames:
if fileName.endswith('.csv'):
zipObj.extract(fileName, self.tempDir)
print('unzip' + str(fileName))

错误:

File "path...", line 166, in ...

with ZipFile(fullfilename, 'r') as zipObj:
UnboundLocalError: local variable 'fullfilename' referenced before assignment

最佳答案

这个错误信息...

UnboundLocalError: local variable 'fullfilename' referenced before assignment

...意味着您在变量 fullfilename 被赋值之前就已经引用了它。


在您的代码块中:

for arquivo in arquivos:
if arquivo.endswith(".zip"):
fullfilename = os.path.join(caminho_path, arquivo)

仅当条件 arquivo.endswith(".zip")true 时,变量 fullfilename 才会被赋值/强>。否则变量 fullfilename 保持未分配

在其中一个变量未分配的情况下,您尝试在代码的后面部分引用它:

with ZipFile(fullfilename, 'r') as zipObj:

即使变量 fullfilename 仍然未分配。因此您会看到错误。

关于python - 未绑定(bind)本地错误 : local variable 'fullfilename' referenced before assignment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59291834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com