Technology Learning

导航

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

3-8

 1 #3-8
 2 "makeTextFile.py -- create text file"
 3 
 4 import os
 5 ls = os.linesep
 6 
 7 #get filename
 8 fname = raw_input()
 9 while True:
10 
11   if os.path.exists(fname):
12     print "ERROR: '%s' already exists" % fname
13   else:
14     break
15 all = []
16 print "\nEnter lines ('.' by itself to quit).\n"      
17 
18 while True:
19   entry = raw_input('newline:')
20   if entry == '.':
21     break
22   else:
23     all.append(entry)
24 
25 fobj = open(fname, 'w')
26 fobj.writelines(['%s%s' % (x, ls) for x in all])
27 fobj.close()
28 print "DONE!" 
View Code

 

3-10

 1 #3-10
 2 "readtextFile.py---read and display text"
 3 import os
 4 
 5 fname = raw_input("Enter file name:")
 6 print
 7 
 8 if os.path.exists(fname):
 9   fobj = open(fname, 'r')
10   for eachline in fobj:
11     print eachline.strip()
12   fobj.close()
13 else:
14   print "file not exists!!"  
15 
16 #3-11
17 "readtextFile.py---read and display text"
18 
19 fname = raw_input("Enter file name:")
20 print
21 
22 try:
23   fobj = open(fname, 'r')
24 except IOError, e:
25   print "*** file open error: ", e
26 else:
27   for eachline in fobj:
28     print eachline.strip()
29   fobj.close()  
View Code

 

3-12

 1 #3-12
 2 "readtextFile.py---read and display text"
 3 import os
 4 ls = os.linesep
 5 
 6 print """Enter your option:
 7 (w)create a file
 8 (r)read a file
 9 (others)exit
10 """
11 
12 def WriteFile(str):
13   all = []
14   if os.path.exists(str):
15     print "ERROR: '%s' already exists" % fname
16     return
17   
18   print "\nEnter lines ('.' by itself to quit).\n"
19   while True:
20       entry = raw_input('newline:')
21       if entry == '.':
22         break
23       else:
24         all.append(entry)
25 
26     fobj = open(fname, 'w')
27     fobj.writelines(['%s%s' % (x, ls) for x in all])
28     fobj.close()
29     print "DONE!" 
30 
31 def ReadFile(str):
32   #fname = raw_input("Enter file name:")
33   print
34   
35   if os.path.exists(str):
36       fobj = open(str, 'r')
37       for eachline in fobj:
38         print eachline.strip()
39       fobj.close()
40   else:
41       print "file not exists!!" 
42     
43   
44 opt =raw_input("your option is:")
45 
46 if(opt == 'w'):
47   fname = raw_input("the file to write is: ")
48   WriteFile(fname)
49 elif opt== 'r':
50   fname = raw_input("the file to read is: ")   
51   ReadFile(fname)
52 else:
53   print "exit" 
View Code

 

3-13

 1 #3-13
 2 "readtextFile.py---read and display text"
 3 import os
 4 ls = os.linesep
 5 
 6 print """Enter your option:
 7 (w)create a file
 8 (r)read a file
 9 (m)modify a file
10 (others)exit
11 """
12 
13 def WriteFile(str):
14   all = []
15   if os.path.exists(str):
16     print "ERROR: '%s' already exists" % fname
17     return
18   
19   print "\nEnter lines ('.' by itself to quit).\n"
20   while True:
21       entry = raw_input('newline:')
22       if entry == '.':
23         break
24       else:
25         all.append(entry)
26 
27     fobj = open(fname, 'w')
28     fobj.writelines(['%s%s' % (x, ls) for x in all])
29     fobj.close()
30     print "DONE!" 
31 
32 def ReadFile(str):
33   #fname = raw_input("Enter file name:")
34   print
35   
36   if os.path.exists(str):
37       fobj = open(str, 'r')
38       for eachline in fobj:
39         print eachline.strip()
40       fobj.close()
41   else:
42       print "file not exists!!" 
43 
44 def ModifyFile(str):
45   all = []
46   if os.path.exists(str):
47     i = 1
48     fobj = open(str, 'r')
49     for eachline in fobj:
50       newline = eachline.strip()
51       print "line %d: %s" % (i,newline)
52       newline = raw_input("replace with:")    
53       all.append(newline)
54       i+=1
55     fobj.close()
56     
57     opt = raw_input("do you want to save your changes,\ny(yes) \nother(n):")
58     if opt == 'y':    
59       output = open(str, 'w')
60       output.writelines(['%s%s' % (x, ls) for x in all])
61       output.close()
62     else:
63       return  
64   else:
65     print "file not exists!!"       
66   
67 opt =raw_input("your option is:")
68 
69 if(opt == 'w'):
70   fname = raw_input("the file to write is: ")
71   WriteFile(fname)
72 elif opt== 'r':
73   fname = raw_input("the file to read is: ")   
74   ReadFile(fname)
75 elif opt == 'm':
76   fname = raw_input("the file to modify is:")
77   ModifyFile(fname)
78   print "the new file content is:" 
79   ReadFile(fname) 
80 else:
81   print "exit" 
View Code

 

posted on 2013-08-02 09:58  浔阳渔夫  阅读(283)  评论(0编辑  收藏  举报