码迷,mamicode.com
首页 > 其他好文 > 详细

异常处理

时间:2019-04-02 10:47:26      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:name   NPU   for   nal   循环   ioerror   finally   error:   process   

检测文件名是否正确,然后按行读取文件,否则循环输入文件名直至正确。

def processFile(dataFile):
    count = 1
    for item in dataFile:
        print(‘Line ‘+ str(count) + ‘:‘ + item.strip())
        count = count + 1

while True:
    try:
        fileName = input(‘Input a file name to open: ‘)
        dataFile = open(fileName,‘r‘)
    except IOError:
        print(‘Bad file name; try again‘)
    else:
        processFile(dataFile)
        break
    finally:
        try:
            dataFile.close()
        except NameError:
            print(‘Going around again‘)

 

异常处理

标签:name   NPU   for   nal   循环   ioerror   finally   error:   process   

原文地址:https://www.cnblogs.com/ACPIE-liusiqi/p/10640822.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!