文件的操作,创建一个file类的实例,使用readline方法读文件的每一行
程序代码:
$ ./t.py
Programming is fun
When the work is donw
If you wanna make your work also fun:
use Python!

#!/usr/bin/python poem = '''\ Programming is fun When the work is done if you wanna make your work also fun: use Python! ''' f = file('poem.txt', 'w') # open for 'w'riting f.write(poem) # write text to file f.close() # close the file f = file('poem.txt') # 如果不指定模式,默认为读模式 while True: line = f.readline() if len(line) == 0: # 表明到了EOF break print line, # 逗号是为了避免python 自动添加新行 f.close()
$ ./t.py
Programming is fun
When the work is donw
If you wanna make your work also fun:
use Python!

The quieter you become, the more you can hear