![](/img/trans.png)
[英]UnboundLocalError: local variable 'truebomb' referenced before assignment
[英]UnboundLocalError: local variable 'txt' referenced before assignment
到目前為止,這是我的程序。 運行它時,出現錯誤:“ UnboundLocalError:分配前引用了本地變量'txt'”。 我嘗試在txt之前添加global,以將其聲明為全局變量,但是這樣做時又遇到了另一個錯誤。 有什么想法我做錯了嗎? 提前致謝。
def getwords():
#function to get words in the input file
try:
global txt
txt=open("sample.txt",'r')
except IOError:
print('Unable to open file')
words=[]
#read the file line by line
for line in txt:
#convert each line into words with space as delimiter
words=words+line.split()
return words
def wordcount(wordlist):
#function to count words in the file
#worddic is dictionary to store words frequency
worddic=dict()
for x in wordlist:
#convert word to lowercase to ignorecase
t=x.lower()
if(t not in worddic):
worddic[t]=0
worddic[t]=worddic[t]+1
max=-1
t=''
for x in worddic:
if(worddic[x]>max):
max=worddic[x]
t=x
return t
def letters(wordlist,lettercount):
#function to count letters in the file
for x in wordlist:
#For each word in the list
t=x.lower()
for y in t:
#for each letter in the word
if(not (y in lettercount)):
#if the letter is not in dictionary add it
#and set frequency to zero
lettercount[y]=0
#increment the frequency of letter in dictionary
lettercount[y] = lettercount[y]+1
def createoutput(lettercount,wordlist,mostword):
#creates an empty file 'statistics.txt'
try:
txt2=open("statistics.txt",'w+')
except IOError:
print('Unable to create file')
txt2.write('Number of words in the file are '+str(len(wordlist))+'\n')
txt2.write('Most repeated word in the file is '+mostword+'\n')
for x in lettercount:
#write to the file 'statistics.txt'
txt2.write(x+' appeared in the file for '+str(lettercount[x])+' times \n')
def main():
wordlist=getwords()
#lettercount is a dictionary with letters as keys
#and their frequency in the input file as data
lettercount=dict()
mostword=wordcount(wordlist)
letters(wordlist,lettercount)
createoutput(lettercount,wordlist,mostword)
main()
當open()
調用失敗時,您將吞下異常:
try:
global txt
txt=open("sample.txt",'r')
except IOError:
print('Unable to open file')
現在txt
從未分配給 ,因為它是open()
調用,此處失敗。 而不是繼續執行該功能,您應該在此時返回:
try:
txt=open("sample.txt",'r')
except IOError:
print('Unable to open file')
return
您想像這樣構造異常:
try:
# normal code here
except:
# handle the exception
例如,此代碼是錯誤的,因為它運行的代碼可能會在異常處理程序之外失敗
def createoutput(lettercount,wordlist,mostword):
#creates an empty file 'statistics.txt'
try:
txt2=open("statistics.txt",'w+')
except IOError:
print('Unable to create file')
txt2.write('Number of words in the file are '+str(len(wordlist))+'\n')
txt2.write('Most repeated word in the file is '+mostword+'\n')
將普通讀取移到異常處理程序中將如下所示
def createoutput(lettercount,wordlist,mostword):
#creates an empty file 'statistics.txt'
try:
txt2=open("statistics.txt",'w+')
txt2.write('Number of words in the file are '+str(len(wordlist))+'\n')
txt2.write('Most repeated word in the file is '+mostword+'\n')
except IOError:
print('Unable to create file')
但是您從未關閉過文件。 您將close文件命令放在哪里? 顯然,將其打開后,如果txt2.write引發異常怎么辦? 如果在那里發生其他問題怎么辦? 你永遠不會關閉!
對於文件,您將需要執行此操作,因此當文件離開作用域時將其關閉
with open("statistics.txt", "w+") as statsfile:
statsfile.write("Number of words ...
當然,您將其放入異常處理程序中
def getwords(filename="sample.txt"):
words = []
try:
with open (filename, 'r') as txt:
for line in txt:
words = words + line.split()
except IOError:
print "unable to open file"
return words
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.