《Python核心编程》部分错误纠正(勘误表)(持续更新)
Chapter 3:
例3-1 makeTextFile.py
#!/usr/bin/env python 'makeTextFile.py' import os ls = os.linesep #get File name while True: fname = raw_input("Enter file name: ") if os.path.exists(fname): print "ERROR: '%s' already exists" % fname else: break #get file contents lines all = [] print "\nEnter lines('.' by itself to quit).\n" #loop until user terminate input 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.write('\n',join(all)) fobj.close() print "DONE"
例3-2 readTextFile.py
#!/usr/bin/env python 'readTextFile.py' #get filename fname = raw_input('Enter filename: ') print #attempt to open file for reading try: fobj = open(fname,'r') except IOError, e: print "*** file open error:", e else: #display contents to the screen for eachLine in fobj: print eachLine, fobj.close()
作者:whatbeg
出处1:http://whatbeg.com/
出处2:http://www.cnblogs.com/whatbeg/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
更多精彩文章抢先看?详见我的独立博客: whatbeg.com