python文件操作

时间:2022-05-06
本文章向大家介绍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()