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