欢迎来到天天文库
浏览记录
ID:52244940
大小:103.66 KB
页数:10页
时间:2020-03-25
《python核心编程第九章.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#-*-coding:utf-8-*-importospath=os.getcwd()deffiler(path,fname):"""9–1.文件过滤.显示一个文件的所有行,忽略以井号(#)开头的行."""f=open(os.path.join(path,fname),'r')forreaderinf:ifnotreader.startswith('#'):printreaderf.close()defprintLine(n,f):"""9–2.文件访问.提示输入数字N和文件F,然后显示文件F的前N行."""f=r
2、aw_input('Pleaseenterfilename:').strip()n=int(raw_input('Pleaseenterlinesnumber:').strip())fopen=open(os.path.join(path,f),'r')forreaderinfopen:cont+=1ifcont3、n(path,f),'r')printlen(fopen.readlines())fopen.closedefpages():"""9–4.文件访问.写一个逐页显示文本文件的程序.提示输入一个文件名,每次显示文本文件的25行,暂停并向用户提示"按任意键继续.",按键后继续执行."""cont=0f=raw_input('Pleaseenterfilename:').strip()fopen=open(os.path.join(path,f),'r')forreaderinfopen:cont+=1ifcont>254、:cont=1raw_input('Pleasepressanykeytocontinue...')printreaderfopen.close()defcompareFile(f,f2):"""9–6.文件比较.写一个比较两个文本文件的程序.如果不同,给出第一个不同处的行号和列号."""fopen_1=open(os.path.join(path,f),'r')read_1=fopen_1.readlines()fopen_2=open(os.path.join(path,f2),'r')read_2=fopen5、_2.readlines()minNum=min(len(read_1),len(read_2))foriinrange(minNum):minRow=min(len(read_1[i]),len(read_2[i]))forjinrange(minRow):ifcmp(read_1[i][j],read_2[i][j])!=0:print'Differcontents>>Line:%d-Row:%d'%(i+1,j+1)breakforiinrange(1,abs(len(read_1)-len(read_2))6、+1):print'Differcontent>>Line:%d-Row:1'%(minNum+i)fopen_1.close()fopen_2.close()importConfigParserdefconfigFile(fn):"""9–7.解析文件.Win32用户:创建一个用来解析Windows.ini文件的程序."""cp=ConfigParser.ConfigParser()cp.read(os.path.join(path,fn))forsectionincp.sections():printcp.it7、ems(section)forkeyincp.options(section):print'[%s]-%s=%s'%(section,key,cp.get(section,key))cp.add_section('Name')cp.set('Name','kyrie','27')fp=open(os.path.join(path,fn),'a')cp.write(fp)fp.close()defviewModul():name=raw_input('modulename:')obj=__import__(name)8、nature=dir(obj)foriteminnature:print'name:%s,type:%s,value:%s'%(item,type(getattr(obj,item)),getattr(obj,item))importosimportgcdeffindDoc(path):"""9–9.Python文档字符串.进入Python标准库所在的目录
3、n(path,f),'r')printlen(fopen.readlines())fopen.closedefpages():"""9–4.文件访问.写一个逐页显示文本文件的程序.提示输入一个文件名,每次显示文本文件的25行,暂停并向用户提示"按任意键继续.",按键后继续执行."""cont=0f=raw_input('Pleaseenterfilename:').strip()fopen=open(os.path.join(path,f),'r')forreaderinfopen:cont+=1ifcont>25
4、:cont=1raw_input('Pleasepressanykeytocontinue...')printreaderfopen.close()defcompareFile(f,f2):"""9–6.文件比较.写一个比较两个文本文件的程序.如果不同,给出第一个不同处的行号和列号."""fopen_1=open(os.path.join(path,f),'r')read_1=fopen_1.readlines()fopen_2=open(os.path.join(path,f2),'r')read_2=fopen
5、_2.readlines()minNum=min(len(read_1),len(read_2))foriinrange(minNum):minRow=min(len(read_1[i]),len(read_2[i]))forjinrange(minRow):ifcmp(read_1[i][j],read_2[i][j])!=0:print'Differcontents>>Line:%d-Row:%d'%(i+1,j+1)breakforiinrange(1,abs(len(read_1)-len(read_2))
6、+1):print'Differcontent>>Line:%d-Row:1'%(minNum+i)fopen_1.close()fopen_2.close()importConfigParserdefconfigFile(fn):"""9–7.解析文件.Win32用户:创建一个用来解析Windows.ini文件的程序."""cp=ConfigParser.ConfigParser()cp.read(os.path.join(path,fn))forsectionincp.sections():printcp.it
7、ems(section)forkeyincp.options(section):print'[%s]-%s=%s'%(section,key,cp.get(section,key))cp.add_section('Name')cp.set('Name','kyrie','27')fp=open(os.path.join(path,fn),'a')cp.write(fp)fp.close()defviewModul():name=raw_input('modulename:')obj=__import__(name)
8、nature=dir(obj)foriteminnature:print'name:%s,type:%s,value:%s'%(item,type(getattr(obj,item)),getattr(obj,item))importosimportgcdeffindDoc(path):"""9–9.Python文档字符串.进入Python标准库所在的目录
此文档下载收益归作者所有