Python代码奇怪地跳转到except块

Python代码奇怪地跳转到except块,python,Python,所以我在这里发疯,试图弄清楚发生了什么,我添加了大量的打印语句来查看代码发生了什么,我无法理解为什么它会像这样跳跃 我知道代码看起来很黑,确实如此,但我看不出为什么它会跳入块中 def add_audio(self, word): '''function for adding audio path to a word''' print "add audio function" path = "mypath/%s/" % (self.language_ISO) pr

所以我在这里发疯,试图弄清楚发生了什么,我添加了大量的打印语句来查看代码发生了什么,我无法理解为什么它会像这样跳跃

我知道代码看起来很黑,确实如此,但我看不出为什么它会跳入块中

def add_audio(self, word):
    '''function for adding audio path to a word'''
    print "add audio function"
    path = "mypath/%s/" % (self.language_ISO)
    print 'makinf file list' 
    existing_file = glob.glob("%s/%s.*" % (path, word)) + glob.glob('%s/%s[0-9].*' % (path, word))
    print ' doing if statement'
    print existing_file
    if existing_file:
        print 'into if statement'
        if self.choice("\nThere is an existing audio file for this word...\nWould you like to add another?: "):
            add_another = True
            print add_another
        else:   
            return (existing_file, "N/A")
    print 'before try'
    try:
        print "inside try"
        if add_another:
            print 'about to raise error'
            #Made this variable and thre an error to force the except loop to run and add another file
            raise NameError('throwing an intentional error as a hack')
        print 'made it past try but failed somewhere else'
        wiktionary_page = urllib2.urlopen("http://%s.wiktionary.org/wiki/FILE:en-us-%s.ogg" % (self.wiktionary_prefix, word))
        print 'made it past try but failed somewhere fromstring'
        wiktionary_page = fromstring(wiktionary_page.read())
        print 'made it past try but failed somewhere xpath'
        file_URL = wiktionary_page.xpath("//*[contains(concat(' ', @class, ' '), ' fullMedia ')]/a/@href")[0]
        print 'made it past try but failed somewhere wget'
        os.popen("wget -O %s/%s.ogg --progress=bar 'http:%s'" % (path, word, file_URL))
        print("\nFile Downloaded from wiktionary successfully!\n")
        return ("%s/%s.ogg" % (path, word), file_URL)
    except:
        if self.choice("Is there an audio file_URL ready?: "):
            file_URL = raw_input("What is the file_URL?: ")
            print "\n%s\n" % (file_URL)
            while not self.choice("Is this correct?: "):
                file_URL = raw_input("What is the file_URL")
                time.sleep(.5)
                print "\n%s\n" % (file_URL)
            file_extension = file_URL.split('.')[-1]
            file_number = len(existing_file)
            file_path_name = "%s/%s%d.%s" % (path, word, file_number, file_extension)
            os.popen("wget -O %s --progress=bar '%s'" % (file_path_name, file_URL))
            return ("%s/%s%d.%s" % (file_path_name, file_URL))
        elif not existing_file:
            with open(self.logfile, 'a') as f:
                f.write("AUDIO: Need to record audio for %s\n" % (word))
            return ("/", "N/A")
我得到的输出是:

add audio function
makinf file list
doing if statement
[]
before try
inside try
Is there an audio file_URL ready?:
代码如下,似乎缺少print语句,只是在某个地方分解并跳转到except块

def add_audio(self, word):
    '''function for adding audio path to a word'''
    print "add audio function"
    path = "mypath/%s/" % (self.language_ISO)
    print 'makinf file list' 
    existing_file = glob.glob("%s/%s.*" % (path, word)) + glob.glob('%s/%s[0-9].*' % (path, word))
    print ' doing if statement'
    print existing_file
    if existing_file:
        print 'into if statement'
        if self.choice("\nThere is an existing audio file for this word...\nWould you like to add another?: "):
            add_another = True
            print add_another
        else:   
            return (existing_file, "N/A")
    print 'before try'
    try:
        print "inside try"
        if add_another:
            print 'about to raise error'
            #Made this variable and thre an error to force the except loop to run and add another file
            raise NameError('throwing an intentional error as a hack')
        print 'made it past try but failed somewhere else'
        wiktionary_page = urllib2.urlopen("http://%s.wiktionary.org/wiki/FILE:en-us-%s.ogg" % (self.wiktionary_prefix, word))
        print 'made it past try but failed somewhere fromstring'
        wiktionary_page = fromstring(wiktionary_page.read())
        print 'made it past try but failed somewhere xpath'
        file_URL = wiktionary_page.xpath("//*[contains(concat(' ', @class, ' '), ' fullMedia ')]/a/@href")[0]
        print 'made it past try but failed somewhere wget'
        os.popen("wget -O %s/%s.ogg --progress=bar 'http:%s'" % (path, word, file_URL))
        print("\nFile Downloaded from wiktionary successfully!\n")
        return ("%s/%s.ogg" % (path, word), file_URL)
    except:
        if self.choice("Is there an audio file_URL ready?: "):
            file_URL = raw_input("What is the file_URL?: ")
            print "\n%s\n" % (file_URL)
            while not self.choice("Is this correct?: "):
                file_URL = raw_input("What is the file_URL")
                time.sleep(.5)
                print "\n%s\n" % (file_URL)
            file_extension = file_URL.split('.')[-1]
            file_number = len(existing_file)
            file_path_name = "%s/%s%d.%s" % (path, word, file_number, file_extension)
            os.popen("wget -O %s --progress=bar '%s'" % (file_path_name, file_URL))
            return ("%s/%s%d.%s" % (file_path_name, file_URL))
        elif not existing_file:
            with open(self.logfile, 'a') as f:
                f.write("AUDIO: Need to record audio for %s\n" % (word))
            return ("/", "N/A")

我应该做的正是@smarx所说的。我应该这样打印异常

except Exception as e:
    print str(e)

这将打印引发的异常,并帮助我调试出错的地方。除此之外,我应该自定义处理不同类型的异常

我猜:
add\u另一个
不存在。但是你可以确定你是否捕获了异常并打印出来(或者根本没有捕获)。天哪,我不敢相信我错过了,谢谢(我将你的问题编辑为“除块”而不是“除循环”)。将最后一个
elif
更改为
else
,我怀疑你会看到一些事情发生。通常,这是个好主意™ 使用命名异常;“裸”
except
块很少有用(除非它们位于一组命名的
except
块的末尾,即使这样,它们通常也应该打印一些有用的信息和/或重新引发异常),调试时,我只需注释掉我的
try
除外
即可查看错误。但是,尽管如此,带有
打印
除外的e
是一个更优雅的解决方案。