首页 > 编程语言 > 详细

20190323——HeadFirestPython学习之异常处理

时间:2019-03-23 18:13:41      阅读:138      评论:0      收藏:0      [点我收藏+]
man=[]
other=[]
try:
    data=open(sketch.txt)
    for each_line in data:
        try:
            (role,line_spoken)=each_line.split(:,1)
            line_spoken=line_spoken.strip()
            if role==A:
                man.append(line_spoken)
            elif role==B:
                other.append(line_spoken)
        except ValueError:
            pass
    data.close()
except IOError:
    print "the data file is missing"

    
try:
    with open(man_data.txt,"w") as out:
        out.write(str(man))
except IOError:
    print "the second missing"

写入文件用WITH代替try except finally
try:
    with open(‘man_data.txt‘,"w") as out:
        out.write(str(man))
except IOError:
    print "the second missing"

 

如果有二个文件可以用,隔开

with open(‘man_data.txt‘,‘w‘) as out, with open(‘other_data.txt‘,‘w‘) as out1:

    out.write(str(man))

    out.write(str(othe))

_______________________________________________

try:
    out=open(‘man_data.txt‘,"w")
    out.write(str(man))
except IOError as err:
    print ‘File error:‘+str(err)
finally:
    if ‘data‘ in locals():
        out.close()

 

20190323——HeadFirestPython学习之异常处理

原文:https://www.cnblogs.com/kellywu/p/10584863.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!