#!/usr/bin/python
import time
try:
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的语法就大部分完成了,剩下的就是多使用,体验其美妙之处,以及熟悉库的使用。