python第28篇--异常

来源:互联网 发布:it项目经理岗位职责 编辑:程序博客网 时间:2024/06/15 02:16
#!/usr/bin/pythonimport timetry:    f = open('poem.txt')    while True:        line = f.readline()        if len(line) == 0:            break        print(line,end=' ')        time.sleep(2)except KeyboardInterrupt:    print('!!You cancelled the reading from the file')finally:    f.close()    print('(cleaning up:closed the file)')


结果:

ang@ubuntu:a_byte_python->./7finally.py  programming is fun when the work is done if you wanna make your work also fun: usr python! (cleaning up:closed the file)ang@ubuntu:a_byte_python->./7finally.py  programming is fun when the work is done^C !!You cancelled the reading from the file(cleaning up:closed the file)

到这里基本上python的语法就大部分完成了,剩下的就是多使用,体验其美妙之处,以及熟悉库的使用。

原创粉丝点击