python 异常处理
当前位置:以往代写 > Python教程 >python 异常处理
2019-06-14

python 异常处理

python 异常处理
python">def main():
    try:
        for line in readfile('lines.txt'): print(line.strip()) 
    except IOError as e:
        print('could not open the file:',e)
    except ValueError as e:
        print('bad filename:',e)    

def readfile(filename):
    if filename.endswith(".txt"):        
        fh = open(filename)
        return fh.readlines()
    else: raise ValueError("file must be ends with .txt")

if __name__ == "__main__": main()

标签:python

    关键字:

在线提交作业