Core Python上的第一个程序

来源:互联网 发布:山东淘宝代运营 编辑:程序博客网 时间:2024/04/27 14:40

Core Python上的第一个程序,书上的排版尤其是针对代码这块最令我不爽,看来看去,这个缩进很明显的错误~还是要自己改改,调调才可,尽信书不如无书,也好~!

#!/usr/bin/env python#'makeTextFile.py  -- create text file'import osls= os.linesep#get filenamewhile True:fname = raw_input('input file name:')if os.path.exists(fname):print "Error: '%s' already exists" % fnamebreakelse:#get file contentall = []print "\nEnter lines ('.' by itself to quit).\n"#loop until user terminates inputwhile True:entry = raw_input('>')if entry == '.':breakelse:all.append(entry)#write lines to file with proper line-endingprint 'file name:%s' % fnamefobj = open(fname, 'w')fobj.writelines(['%s%s' % (x, ls) for x in all])fobj.close()print 'Done!'print 'Exit~!'


#!/usr/bin/env python'readTextFile.py  -- read text file'#get filenamefname = raw_input('Enter filename:')print#attemp to open file for readingtry:fobj = open(fname, 'r')except IOError, e:print "***file open failed:",eelse:#print file contentfor eachline in fobj:print eachline,fobj.close()

一个简单的文件读写程序~~