[Python实战0]异常的处理


在上一篇文章中,我们使用python进行文件的读取和数据的简单处理, 但是在处理的时候我们遇到了一个问题,就是没有判断一行中是否出现了冒号从而导致出现了错误,最后我们在之前的代码中加入了if语句进行判断,从而避免了错误的发生,代码如下:
data = open('sketch.txt')

for each_line in data:
    if not each_line.find(":")==-1:
        
        (role,line_spoke) = each_line.split(":",1)
        print(role,end='')
        print(' said: ',end='')
        print(line_spoke,end='')


data.close()

当然,我们这里也可以不加入if语句进行处理这个错误,我们可以使用异常机制来规避这个错误,可以修改以上的代码,如下:

 

data = open('sketch.txt')

for each_line in data:
    try:
        (role,line_spoke) = each_line.split(":",1)
        print(role,end='')
        print(' said: ',end='')
        print(line_spoke,end='')
    except:
        pass
    
data.close()
注意以上代码,我们加入了try和except来进行异常的捕捉和处理,最后使用pass语句进行忽略掉这条语句,运行代码,是可以正常打印之前的数据的,如下:
>>> ================================ RESTART ================================
>>> 
Man said:  Is this the right room for an argument?
Other Man said:  I've told you once.
Man said:  No you haven't!
Other Man said:  Yes I have.
Man said:  When?
Other Man said:  Just now.
Man said:  No you didn't!
Other Man said:  Yes I did!
Man said:  You didn't!
Other Man said:  I'm telling you, I did!
Man said:  You did not!
Other Man said:  Oh I'm sorry, is this a five minute argument, or the full half hour?
Man said:  Ah! (taking out his wallet and paying) Just the five minutes.
Other Man said:  Just the five minutes. Thank you.
Other Man said:  Anyway, I did.
Man said:  You most certainly did not!
Other Man said:  Now let's get one thing quite clear: I most definitely told you!
Man said:  Oh no you didn't!
Other Man said:  Oh yes I did!
Man said:  Oh no you didn't!
Other Man said:  Oh yes I did!
Man said:  Oh look, this isn't an argument!
Other Man said:  Yes it is!
Man said:  No it isn't!
Man said:  It's just contradiction!
Other Man said:  No it isn't!
Man said:  It IS!
Other Man said:  It is NOT!
Man said:  You just contradicted me!
Other Man said:  No I didn't!
Man said:  You DID!
Other Man said:  No no no!
Man said:  You did just then!
Other Man said:  Nonsense!
Man said:  (exasperated) Oh, this is futile!!
Other Man said:  No it isn't!
Man said:  Yes it is!
>>> 
同样的,由于在开始时进行文件的打开时有可能找不到文件,会出现错误,如下错误:
>>> 
Traceback (most recent call last):
  File "D:\python\file\sketch.py", line 1, in 
    data = open('sketch1.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'sketch1.txt'
>>> 
提示无法找到文件,当然我们可以在文件打开之前增加一个文件是否存在的判断,代码如下:
import os

if os.path.exists('sketch.txt'):
    data = open('sketch.txt')

    for each_line in data:
        
        try:
            
            (role,line_spoke) = each_line.split(":",1)
            print(role,end='')
            print(' said: ',end='')
            print(line_spoke,end='')
        except:
            pass
    
    data.close()
else:
    print('The file is not exists')
这里我们加入了if语句进行判断当前的文件是否存在,如果存在才去读取文件,否则打印文件不存在,当然我们也可以通过异常的机制进行处理,代码如下:
try:
    data = open('sketch.txt')

    for each_line in data:
        
        try:
            
            (role,line_spoke) = each_line.split(":",1)
            print(role,end='')
            print(' said: ',end='')
            print(line_spoke,end='')
        except:
            pass
    
    data.close()
except:
    print('The file is not exists')
当然,我们也可以接收特定类型的错误,比如,如果文件找不到会产生IOError,我们则可以通过IOError进行接收这个错误,如下:
try:
    data = open('sketch.txt')

    for each_line in data:
        
        try:
            
            (role,line_spoke) = each_line.split(":",1)
            print(role,end='')
            print(' said: ',end='')
            print(line_spoke,end='')
        except:
            pass
    
    data.close()
except IOError:
    print('The file is not exists')
这里就先简单的介绍下异常的基本使用,更深入的使用会在下一篇博文中介绍。


 

评论关闭