3–11.
字符串格式化 不再抑制 readTextFile.py 中 print 语句生成的 NEWLINE 字符,修改你的
代码, 在显示一行之前删除每行末尾的空白。这样, 你就可以移除 print 语句末尾的逗号了。
提示: 使用字符串对象的 strip()方法
3–12. 合并源文件。将两段程序合并成一个,给它起一个你喜欢的名字,比方
readNwriteTextFiles.py。让用户自己选择是创建还是显示一个文本文件。
3–13. 添加新功能。将你上一个问题改造好的 readNwriteTextFiles.py 增加一个新功
能:允许用户编辑一个已经存在的文本文件。 你可以使用任何方式,无论是一次编辑一行,还
是一次编辑所有文本。需要提醒一下的是, 一次编辑全部文本有一定难度,你可能需要借助 GUI
工具包或一个基于屏幕文本编辑的模块比如 curses 模块。要允许用户保存他的修改(保存到
文件)或取消他的修改(不改变原始文件),并且要确保原始文件的安全性(不论程序是否正
常关闭)。
import os ls=os.linesep #get filename def write_Text(): fname=raw_input(">>") while True: if os.path.exists(fname): print "ERROR:'%S' already exists" % fname else: break all=[] print "/nEnter lines('.' by itself to quit).\n" while True: entry=raw_input('>') if entry=='.': break else: all.append(entry) #write lines to file with proper line-ending fobj=open(fname,'w') #fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.writelines([x+ls for x in all]) fobj.close() print'DONE!' def read_Text(): fname=raw_input("Enter filename:") print try: fobj=open(fname,'r') except IOError,e: print "*** file open error:",e else: #display contents to the screen for eachLine in fobj: eachLine=eachLine.strip().strip(',') print eachLine fobj.close() def modify_text(): fname=raw_input("Enter filename:") fobj=open(fname,'r+') lis=fobj.readlines() i=0 for eachline in lis: eachline=eachline.strip() print str(i)+" "+eachline i+=1 while True: print "which line you want to modify:" num=raw_input(">>") num=int(num) lis[num]=raw_input("write new content:")+'\n' fobj=open(fname,'w+') fobj.writelines(lis) fobj.close() print "modifiy sucess" choice=raw_input("exit? y/n:") if choice=="n": break else: continue while True: print print "choice 1,write file" print "choice 2,read file" print "choice 3,modify file" print choice=raw_input("please make a choice:") if choice=="1": write_Text() elif choice=="2": read_Text() elif choice=="3": modify_text() else: print "please enter 1 or 2"
修改后:
import os ls=os.linesep #get filename def write_Text(): fname=raw_input("filename:") while os.path.exists(fname): print "ERROR:'%S' already exists" % fname all=[] print "/nEnter lines('.' by itself to quit).\n" entry=raw_input(">>") while entry!=".": all.append(entry) entry=raw_input('>') #write lines to file with proper line-ending fobj=open(fname,'w') #fobj.writelines(['%s%s' % (x,ls) for x in all]) fobj.writelines([x+ls for x in all]) fobj.close() print'DONE!' make_choice() def read_Text(): fname=raw_input("Enter filename:") print "\n" try: fobj=open(fname,'r') except IOError,e: print "*** file open error:",e else: #display contents to the screen for eachLine in fobj: eachLine=eachLine.strip().strip(',') print eachLine fobj.close() make_choice() def modify_text(): fname=raw_input("Enter filename:") fobj=open(fname,'r+') lis=fobj.readlines() i=0 for eachline in lis: eachline=eachline.strip() print str(i)+" "+eachline i+=1 choice="n" while choice=="n": print "which line you want to modify:" num=raw_input(">>") num=int(num) lis[num]=raw_input("write new content:")+'\n' choice=raw_input("exit? y/n:") fobj=open(fname,'w+') fobj.writelines(lis) fobj.close() print "modifiy sucess" make_choice def make_choice(): print "\n" print "choice 1,write file" print "choice 2,read file" print "choice 3,modify file" print "\n" choice=raw_input("please make a choice:") if choice=="1": write_Text() elif choice=="2": read_Text() elif choice=="3": modify_text() else: print "please enter 1 or 2 or 3" make_choice()