python 核心编程课后练习(chapter 3)

3-8

 #3-8
"makeTextFile.py -- create text file" import os
ls = os.linesep #get filename
fname = raw_input()
while True: if os.path.exists(fname):
print "ERROR: '%s' already exists" % fname
else:
break
all = []
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!"

3-10

 #3-10
"readtextFile.py---read and display text"
import os fname = raw_input("Enter file name:")
print if os.path.exists(fname):
fobj = open(fname, 'r')
for eachline in fobj:
print eachline.strip()
fobj.close()
else:
print "file not exists!!" #3-11
"readtextFile.py---read and display text" fname = raw_input("Enter file name:")
print try:
fobj = open(fname, 'r')
except IOError, e:
print "*** file open error: ", e
else:
for eachline in fobj:
print eachline.strip()
fobj.close()

3-12

 #3-12
"readtextFile.py---read and display text"
import os
ls = os.linesep print """Enter your option:
(w)create a file
(r)read 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!!" 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)
else:
print "exit"

3-13

 #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"
上一篇:【BZOJ1822】[JSOI2010]Frozen Nova 冷冻波 几何+二分+网络流


下一篇:django 自定用户系统 以及 Django Model 定义语法