小编sam*_*am 的帖子

如何使用python处理错误?

我在下面的程序中比较两个文件.如果它是相同的那么我打印成功就像失败一样.我正在使用一个名为jenkins的集成工具,在比较文件失败时发送电子邮件,这样做 - 我必须正确处理错误.有人能告诉我如何处理错误吗?

Error_Status=0
def compare_files(file1, file2):
   try:
       with open(file1, 'rb') as f_file1, open(file2, 'rb') as f_file2:
           if f_file1.read() == f_file2.read():
               print 'SUCCESS \n'
               #print 'SUCESS:\n  {}\n  {}'.format(file1, file2)
           else:
               print 'FAILURE \n'
               Error_Status=1
    except IOError:
        print "File is NOT compared"
        Error_Status = 1
Run Code Online (Sandbox Code Playgroud)

詹金斯控制台输出:

E:\Projekte\Audi\Cloud_SOP17_TTS>rem !BUILD step: Execute test: tts.py 

E:\Projekte\Audi\Cloud_SOP17_TTS>call python tts.py file1 file2   || echo failed 
INPUT ENG: I am tired
Latency: 114msec



[ERROR] Can't Create Reference PCM or Response JSON files!
INPUT GED: facebook nachricht schönes …
Run Code Online (Sandbox Code Playgroud)

python python-2.7 jenkins

5
推荐指数
1
解决办法
142
查看次数

AttributeError:'NoneType'对象在python中没有属性'read'

if __name__ == '__main__':
    filename = open('sevi.txt', 'wb')
    content = filename.write("Cats are smarter than dogs")
    for line in content.read(): 
        match = re.findall('[A-Z]+', line)
        print match
    filename.close()
Run Code Online (Sandbox Code Playgroud)

我是python的新手.我只是打开一个文件并在其中写入一些文本.稍后阅读内容通过使用正则表达式查找其中的所有字符.但我收到错误,因为'NoneType'对象没有属性'read'.如果我也使用readlines,我收到错误.

python regex python-2.7

-1
推荐指数
1
解决办法
2100
查看次数

标签 统计

python ×2

python-2.7 ×2

jenkins ×1

regex ×1