python文件操作

#!TestFile.py
# -*- coding: cp936 -*-
poem='''\
Programming is fun
When the work is done
if you want make your work also fun:
user Python!
'''
f=file('D:\\poem.txt','w')#如果不指定路径则默认保存在该代码所保存的文件目录下
f.write(poem)
f.close()
f=file('D:\\poem.txt')
while True:
    line=f.readline()
    if len(line)==0:
        break
    print line
f.close()

你可能感兴趣的:(python文件操作)