0

我有这个代码:

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在底部附近的标记线上。

我究竟做错了什么?

4

1 回答 1

0

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

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

即它甚至没有找到一个closematch。

于 2013-05-20T09:01:16.807 回答