所以我在这里试图弄清楚发生了什么,我已经添加了一些TON的打印语句来查看代码发生了什么,我无法理解为什么它会像它一样跳来跳去。 ..
我知道代码看起来很hacky,而且它是,但我不明白为什么它会跳进except块....
我得到的输出是:
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")
答案 0 :(得分:1)
我应该做的正是@smarx所说的。我应该通过这样做打印异常
except Exception as e:
print str(e)
这将打印正在引发的异常并帮助我调试出错的地方。除此之外,我应该自定义处理不同类型的异常