MemoryGarden's Blog

努力 -----------大能猫

C++博客 首页 新随笔 联系 聚合 管理
  118 Posts :: 11 Stories :: 20 Comments :: 0 Trackbacks

copy 书上的。

python 写文件:

import os
ls 
= os.linesep

#get file name

while True:
    filename 
= raw_input()
    
if(os.path.exists(filename)):
        
print 'the file %s has exists' % (filename)
    
else:
        
break

#get fine context


text 
= []
print;print "enter lines and done by 'END'"

while True:
    line 
= raw_input('')
    
if(line == "END"):
        
break
    
else:
         text.append(line)

#write into the file

fobj 
= open(filename, "w")
fobj.writelines([
'%s%s' % (x, ls) for x in text])
fobj.close()
print 'DONE'

python 读文件:

 1 import os
 2 
 3 filename = raw_input('input the filename : ')
 4 
 5 try:
 6     fobj = open(filename, 'r')
 7 except IOError, e:
 8     print '**********file open error', e
 9 else:
10     for eachLine in fobj:
11         print eachLine
12 
13     fobj.close()
14 
15 




posted on 2009-12-20 17:41 memorygarden 阅读(343) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理