第三章

第10题
#!/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!"



#!/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()

第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:
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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值