2016-07-16 8 views
-1

ここで何が起こっているのか把握しようとしていますが、コードで何が起こっているのかを確認するためにTONを追加しました。それがあるように、なぜそれが、私はコードがハックに見え知っている、そしてそれは、それは除いてブロックにジャンプしている理由私は見ることができない...Pythonコードが奇妙に例外ブロックにジャンプしています

を周りにジャンプしている....

私が手出力これはです:

add audio function 
makinf file list 
doing if statement 
[] 
before try 
inside try 
Is there an audio file_URL ready?: 

コードは次のとおりですが、printステートメントがありません。ちょうどdどこかに統合され、例外ブロックにジャンプしています。

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") 
+5

私の推測: 'add_another'は存在しません。しかし、あなたが例外を捕捉してそれを印刷した(あるいはそれを全くキャプチャしなかった)かどうかを確かめることができます。 – smarx

+0

omg私はそれを逃したと信じられません。 – deltaskelta

+0

( "except loop"の代わりに "except block"の代わりにあなたの質問を編集しました) – smarx

答えて

1

@smarxが言ったことは、私がすべきことでした。

except Exception as e: 
    print str(e) 

これは、発生している例外を出力し、どこが間違っていたかをデバッグするのに役立ちます。その外で私は異なる種類の例外を扱うカスタムであったはずです

+0

いつか(環境によって)、デバッグ中にエラーを見るために 'try'と' except'をコメントアウトします。しかし、「印刷を伴う例外」はもっと優雅な解決策です。 –

関連する問題