"in order to create a file "
import os
ls = os.linesep
while True:
fname = input("please input the name of the file:")
if os.path.exists(fname):
print ("Error:'%s' already exists" % fname)
continue
else:
break
all = []
print ("Enter line ('.' by itself to quit).\n")
while True:
entry = input(">")
if entry == ".":
break
else:
all.append(entry)
fobj = open(fname,'w')
for x in all:
fobj.writelines(['%s%s' % (x,ls)])
fobj.close()
print ('DONE!')
判断一个文件是否存在,若存在则按行输出文件内容到屏幕上
"open a file"
fname = input("Enter filename:")
try:
fobj = open(fname,'r')
except:
print ("'%s' open error or the file does not exit" % fname)
else:
for eachLine in fobj:
print (eachLine)
fobj.close()