保存输入和读取文件【python】

来源:互联网 发布:php程序员自我评价 编辑:程序博客网 时间:2024/06/05 12:06

make_text_file.py

# -*- coding: utf-8 -*-#!/usr/bin/env python"make_text_file.py -- create text file"import osls = os.linesep#get filenamefname = raw_input("input the name:")while True:    if os.path.exists(fname):        print "ERROR:'%s' already exists" %fname    else:        break;#get file content text linesall = []print "\n Enter lines('.' by itself to be quit).\n"#loop until user terminates input while True:    entry = raw_input(">")    if "." == entry:        break    else:        all.append(entry)##write lines to file with proper line-endingfobj = open(fname,"w")fobj.writelines(["%s%s" % (x,ls) for x in all])fobj.close()print "DONE!"


read_text_file.py

#!/usr/bin/env Python"read_text_file.py --read and display text file"#get filenamefname = raw_input("Enter filename:")print try:    fobj = open(fname,"r")except IOError,e:    print "*** file open error:",eelse:    for eachline in fobj:    print eachline,    fobj.close()





0 0