python 核心编程课后练习(chapter 3)
2013-08-02 09:58
489 查看
3-8
View Code
#3-13 "readtextFile.py---read and display text" import os ls = os.linesep print """Enter your option: (w)create a file (r)read a file (m)modify a file (others)exit """ def WriteFile(str): all = [] if os.path.exists(str): print "ERROR: '%s' already exists" % fname return print "\nEnter lines ('.' by itself to quit).\n" while True: entry = raw_input('newline:') 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!" def ReadFile(str): #fname = raw_input("Enter file name:") print if os.path.exists(str): fobj = open(str, 'r') for eachline in fobj: print eachline.strip() fobj.close() else: print "file not exists!!" def ModifyFile(str): all = [] if os.path.exists(str): i = 1 fobj = open(str, 'r') for eachline in fobj: newline = eachline.strip() print "line %d: %s" % (i,newline) newline = raw_input("replace with:") all.append(newline) i+=1 fobj.close() opt = raw_input("do you want to save your changes,\ny(yes) \nother(n):") if opt == 'y': output = open(str, 'w') output.writelines(['%s%s' % (x, ls) for x in all]) output.close() else: return else: print "file not exists!!" opt =raw_input("your option is:") if(opt == 'w'): fname = raw_input("the file to write is: ") WriteFile(fname) elif opt== 'r': fname = raw_input("the file to read is: ") ReadFile(fname) elif opt == 'm': fname = raw_input("the file to modify is:") ModifyFile(fname) print "the new file content is:" ReadFile(fname) else: print "exit"
View Code
相关文章推荐
- python 核心编程课后练习(chapter 2)
- python 核心编程课后练习(chapter 5)
- python核心编程-第5章课后练习答案
- Python核心编程:课后练习5.4
- Python 核心编程笔记_Chapter_3 Note_4 创建python文件(练习文件操作)
- python核心编程第六章练习
- Python 核心编程笔记_Chapter_4_Note_1 对象属性
- python 核心编程 课后习题 9—2
- python 核心编程练习 6--3
- 《Python核心编程》第二版第407页第十三章练习 续六 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第308页第十一章练习 续二 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第308页第十一章练习 续五 -Python核心编程答案-自己做的-
- Python 核心编程笔记_Chapter_6_Note_1 序列_字符串_列表_元组
- Python 编程核心 - Chapter9练习
- python核心编程十三章练习
- 《Python核心编程》第二版第18页第一章练习 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第55页第三章练习 -Python核心编程答案-自己做的-
- 《Python核心编程》第二版第97页第五章练习 续一 -Python核心编程答案-自己做的-
- Python 核心编程 课后练习题(第六章)
- 《Python核心编程》第二版第407页第十三章练习 续五 -Python核心编程答案-自己做的-