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) –