无法访问本地变量

时间:2013-05-20 08:55:19

标签: python-3.x

我有这段代码:

def findwordfiles(title):
    name =[]
    chekname =[]
    title = title.strip()
    found = 0
    searchdir = '/Volumes/public/auryn/Marketing/Metadata/Auryn Reader'
    for dirname, dirnames, filenames in os.walk(searchdir):
        name = list(title)
        i=0
        j=0
        for filename in filenames:
            if filename.endswith((".docx")):
                if ("Auryn_Reader_Meta_Data - " in filename):
                    hellname = filename.replace ("Auryn_Reader_Meta_Data - ","")
                    hellname = hellname.lower()
                    chekname = list(hellname)
                    for i in range (0, len(chekname)):
                        if (j < len(name)):
                            if name[j] == chekname[i]:
                                if j == (len(name)-1):
                                    #raw_input("found! "+ title + " in file" + str(filename) )
                                    target = os.path.join(os.getcwd(), "..") 
                                    shutil.copy2(os.path.join(dirname,filename), target)
                                    found = 1
                                j = j+1
                            i=i+1
                if( j/len(name) > 0.4):
                    if ( j/len(name) > matchpage):
                        closematch = str(filename)
                        matchpage = j/len(name)
                i=0
                j=0
    if found == 0 :
            # THE LINE BELOW GIVES ERROR
        selection = raw_input ("no matches found, Closeset match = " + closematch +"match %age = " + str(matchpage) + " accept? (Y/N)")
        if (selection == 'y' or selection == 'Y'):
            target = os.path.join(os.getcwd(), "..") 
            shutil.copy2(os.path.join(dirname,closematch), target)
        else:
            print "Skipped copying!"

当我运行此操作时,我在底部附近的标记线上收到错误:UnboundLocalError: local variable 'closematch' referenced before assignment

我做错了什么?

1 个答案:

答案 0 :(得分:0)

您尚未初始化匹配。该错误表明程序从未运行过这些行: -

        if( j/len(name) > 0.4):
            if ( j/len(name) > matchpage):
                closematch = str(filename)

即。它甚至找不到匹配。