0
我試圖來處理Python.Some線程使用線程的一些文件之前引用局部變量「F」工作正常,沒有錯誤,但有些是通過以下例外Python的錯誤:UnboundLocalError:分配
Exception in thread Thread-27484:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 504, in run
self.__target(*self.__args, **self.__kwargs)
File "script.py", line 62, in ProcessFile
if f is not None:
UnboundLocalError: local variable 'f' referenced before assignment
而運行我的程序
這裏是Python函數
def ProcessFile(fieldType,filePath,data):
try:
if fieldType == 'email':
fname = 'email.txt'
else:
fname = 'address.txt'
f1 = open(fname,'wb')
for r in data[1:]:
r[1] = randomData(fieldType)
f1.write(r[1])
f1.close()
f = open(filePath,'wb')
writer = csv.writer(f)
writer.writerows(data)
f.close()
try:
shutil.move(filePath,processedFileDirectory)
except:
if not os.path.exists(fileAlreadyExistDirectory):
os.makedirs(fileAlreadyExistDirectory)
shutil.move(filePath,fileAlreadyExistDirectory)
finally:
if f is not None:
f.close()
下面的是我如何通過線程
調用上述功能t = Thread(target=ProcessFile,args=(fieldType,filePath,data))
t.start()
Downvoted無法提供完整的回溯。你應該知道更好的聲譽爲3000.特別是因爲你沒有正確縮進你的代碼。 –
追溯會很好,行號可以真正幫助。 –
[爲什麼當變量有值時會得到UnboundLocalError?](http://docs.python.org/2/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the -variable-has-a-value) –