corepython第九章:文件和输入输出

学习笔记:

OS模块代码示例:

 1 import os
 2 for tmpdir in ('/tmp',r'c:\users\administrator\desktop'):
 3 #如果存在括号里面的目录,则break
 4     if os.path.isdir(tmpdir):
 5         break
 6 #如果不存在,则tmpdir为空值,即False
 7 else:
 8     print 'no temp directory available'
 9     tmpdir=''
10 
11 if tmpdir:
12     os.chdir(tmpdir)    #将路径change到这个目录
13     cwd=os.getcwd() #将路径名称赋值给cwd
14     print '*** current temporary directory'
15     print cwd
16 
17     print '*** creating example directory'
18     os.mkdir('example') #在当前路径下创建一个example的文件夹
19     os.chdir('example') #切换到这个文件夹下的路径
20     cwd=os.getcwd() #将路径赋值给cwd
21     print '*** new working directory:'
22     print cwd
23     print '*** original directory listing:'
24     print os.listdir(cwd) #print example文件夹下的文件名
25 
26     print'*** creating test file:'
27     fobj=open('test','w')   #在example文件夹下创建一个test的文件,以写模式打开
28     fobj.write('foo\n')
29     fobj.write('bar\n')
30     fobj.close()
31     print '*** updated directory listing:'
32     print os.listdir(cwd)   #print example文件夹下的文件名
33 
34     os.rename('test','filetest.txt') #将test文件改名
35     print '*** updated directory listing:'
36     print os.listdir(cwd)   #print example文件夹下的文件名
37 
38     path=os.path.join(cwd,os.listdir(cwd)[0])   #将example文件夹路径和filetest.txt文件名合并,也就是filetest.txt的完整路径
39     print '*** fulle file pathname'
40     print path
41     print '*** (pathname,basename)=='
42     print os.path.split(path) #将路径分开成文件夹路径和文件名
43     print '**8 (filename,extension)=='
44     print os.path.splitext(os.path.basename(path))  #将文件名split生成一个元组,得到('filetest', '.txt')
45 
46     print '*** displaying file contents:'
47     fobj=open(path)
48     for eachline in fobj:
49         print eachline,
50     fobj.close()
51 
52     print '*** deleting test file'
53     os.remove(path) #删除此文件,因为path是此文件的完整路径
54     print '*** updated directory listing:'
55     print os.listdir(cwd) #此时应该为空
56     os.chdir(os.pardir) #返回当前路劲的父路径(上一层路径),也就是桌面
57     print '*** deleting test directory'
58     os.rmdir('example') #删除example文件夹路径
59     print '*** done'
View Code

课后习题

9-6.文件比较.写一个比较两个文本文件的程序,如果不同,给出第一个不同处的行号和列号.

 1 FA=raw_input("please input 1st file's path:")
 2 FB=raw_input("please input 2nd file's path:")
 3 file_a=open(FA,'r')
 4 file_b=open(FB,'r')
 5 a_rl=file_a.readlines()
 6 b_rl=file_b.readlines()
 7 file_a.close()
 8 file_b.close()
 9 MinRow=min(len(a_rl),len(b_rl)) #得到最小行数
10 for r in range(MinRow+1):
11     if a_rl[r]!=b_rl[r]:
12         print 'row:%d' % r+1
13         #得到第一个不同行的最小列数
14         MinCol=min(len(a_rl[r]),len(b_rl[r]))
15         for c in range(MinCol+1):
16             if a_rl[r][c] != b_rl[r][c]:
17                 print 'column:%d' % c+1
18                 break
19         break
View Code

9-9.python文档字符串.进入python标准库所在的目录,检查每个.py文件看是否有_doc_字符串,如果有,对格式进行适当的整理归类.你的程序执行完毕后,应当生成一个漂亮的清单.里边列出哪些模块有文档字符串,以及文档字符串的内容,清单最后附上那些没有文档字符串模块的名字.

 1 import os
 2 #标准库的路径
 3 dir='c:/python27/lib'
 4 #切换到此路径
 5 os.chdir(dir)
 6 #此路径下的所有文件的序列
 7 allfile=os.listdir(dir)
 8 #筛选出类型为py的文件
 9 allpy=[file for file in allfile if file[-3:]=='.py']
10 #定义字典和序列,将有doc的文件存入字典,没有doc的文件存入序列
11 py_doc={}
12 py_no=[]
13 for lj in allpy:
14     f=open(lj,'r')
15     file=f.readlines()
16     #将第一行写入doc
17     doc=file[0][3:]
18     #判定如果第一行的前三个字符为""",则有doc
19     if file[0][0:3]=='"""':
20         #将py的doc写入doc字符串
21         for r in range(1,len(file)):
22             #如果遇到下一个""",则停止写入doc字符串
23             if file[r][0:3]=='"""':
24                 break
25             doc+=file[r]
26         #以文件名为key,doc为value,存入字典
27         py_doc[lj]=doc
28     else:
29         #将没有doc的文件加入到序列中
30         py_no.append(lj)
31 #输出
32 print '***py has doc:'
33 for lj,doc in py_doc.items():
34     print '****************************************',lj,':'
35     print doc
36 print '***py has not doc:'
37 print ' '.join(py_no)
View Code

 

posted @ 2015-04-21 14:14  alvy  阅读(259)  评论(0编辑  收藏  举报