#!/usr/bin/env python
import os
lineEnd = os.linesep
print"Please Input Any Name Of File:",
fileName = raw_input()
try: #chech file exists or not
newFile = open(fileName,'w')
except:
print "File %s Has Existed Or Open Fail!" % fileName
exit()
else:
print "Please Input Any Text , '.' To Quit:",
textForFile = [] #get string of file
while True:
chs = raw_input()
if chs == '.':
break
else:
textForFile.append(chs)
newFile.writelines(['%s%s'%(lines,lineEnd) for lines in textForFile])
newFile.close()
print "Done!"
第11题#!/usr/bin/env python
import os
while True:
print "Please Input File's Name:",
fileName = raw_input()
if os.path.exists(fileName):
break
else:
print "File %s Don't Exist.Please Input Thr Truth Name."
newFile = open(fileName,'r')
for lines in newFile:
print lines,
newFile.close()
#!/usr/bin/env python
import os
while True:
print "Please Input File's Name:",
fileName = raw_input()
if os.path.exists(fileName):
break
else:
print "File %s Don't Exist.Please Input Thr Truth Name."
newFile = open(fileName,'r')
for lines in newFile:
lines.rstrip()
print lines
newFile.close()
第12题
#!/sur/bin/env python
import os
lineEnd = os.linesep
print "A:Creat A New File"
print "B:Read A File That's Had Existed."
chooseLetter = raw_input(">")
if chooseLetter == 'A':
while True:
print"Please Input The File's Name:",
fileName = raw_input()
if os.path.exists(fileName):
print "File %s Has Existed.Please Input Truth Name."
else:
break
textForFile = []
print "Please Input Any Text,'.' To Quit."
while True:
chs = raw_input()
if chs == '.':
break
else:
textForFile.append(chs)
newFile = open(fileName,'w')
newFile.writelines(['%s%s' % (X,lineEnd) for X in textForFile])
newFile.close()
print "Done."
exit()
elif chooseLetter == 'B':
while True:
print "Please Input The Name Of File:",
existFile = raw_input()
if os.path.exists(existFile):
break
else:
print "Can't Find File %s,Please Input Truth Name."%existFile
fileForRead = open(existFile,'r')
for lines in fileForRead:
print lines,
fileForRead.close()
print"Done."
exit()
else:
print"Done."
exit()