写入

#!/usr/bin/env python
import os
ls = os.linesep

fname = raw_input('input a file name:')
#get filename
while True:
    if os.path.exists(fname):
        print "ERROR: '%s' already exists" % fname
        break;
    else:
        print "not exists!"
        break

all = []
print "\nEnter lines('.' by itself to quit).\n"

while True:
    entry = raw_input('>')
    if entry == '.':
        break
    else:
        all.append(entry)

fobj = open(fname,'w')
fobj.writelines(['%s%s' %(x,ls) for x in all])
fobj.close()

print 'Done'



读取


fname = raw_input('Enter filename:')
print

try:
    fobj = open(fname,'r')
except IOError,e:
    print "*** file open error:",e
else:
    for eachLine in fobj:
        print eachLine,
    fobj.close()




data = open('a.txt')
for d in data:
    print(d,end='')
data.close()

fdsaf
fdsa
f
wfeq
fda