尽可能让自己暴露在错误中是一种比较快速的成长方式。发错证明你又要获取新的知识点了。1.编写一个函数sed,它接收以下参数,一个模式字符串,一个替换字符串,和两个文件名;读取第一个文件并将内容写入第二个文件(如果文件中出现任何匹配项)应替换的模式字符串。如果在此过程中遇到错误,您的程序应该捕获异常、打印错误并退出。首先需要抓取关键信息,一个函数,有四个形参,两个字符串,两个文件名;读取文件、写入文件、替换文本和捕获异常。先把这个拆解成最简单的,读和写,因为你之前练习过读写文件,分解它的任务就是把这两个小知识点结合起来,把你掌握的小知识点结合起来。知识点组合的过程就是小知识点的练习也可以形成新的知识点。defsed(file1,file2):fin=open(file1,'r')fout=open(file2,'w')fout.write(fin)sed('words.txt','hello.txt')----------------------------------------------------------------------TypeErrorTraceback(最近调用最后)in67#print('出了点问题')---->8sed('words.txt','hello.txt')insed(file1,file2)3fin=open(file1,'r')4fout=open(file2,'w')---->5fout.write(fin)67#print('Somethingwentwrong')TypeError:write()参数必须是str,而不是_io.TextIOWrapperopen(filename,'r')形式读取的文件内容格式好像是_io.TextIOWrapper,这个不能直接写到别的文件,所以改成了open(filename).read(),这个打印到一个新文件是正常的defsed(file1,file2):fin=open(file1).read()fout=open(file2,'w')fout.write(fin)file1.close()file2。colseAfter()sed('words.txt','hello1.txt')打印正常我想应该关闭两个文件保持好习惯,但是报错----------------------------------------------------------------------------AttributeErrorTraceback(最近一次调用最后一次)in89#print('Somethingwentwrong')--->10sed('words.txt','hello1.txt')insed(file1,file2)4fout=open(file2,'w')5fout.write(fin)---->6file1.close()7file2.colse()8AttributeError:'str'objecthasnoattribute'close'表示传入的参数文件名只在特定的方法或函数中有用,否则为字符串,字符串一定不能关闭然后看了前面练习中用到的fout.close(),接着学习defsed(file1,file2):fin=open(file1).read()fout=open(file2,'w')fout.write(fin)fin.close()fout.colse()sed('words.txt','hello1.txt')-------------------------------------------------------------------------AttributeError回溯(最近调用最后)in89#print('Somethingwentwrong')--->10sed('words.txt','hello1.txt')insed(file1,file2)4fout=open(file2,'w')5fout.write(fin)---->6fin.close()7fout.colse()8AttributeError:'str'objecthasnoattribute'close'这是个大问题。使用open(filename).read()将fin的格式改为字符串,导致无法关闭。然后先把这一行注释掉,看看能不能用defsed(file1,file2):fin=open(file1).read()fout=open(file2,'w')fout.write(fin)#fin.close()fout.colse()sed('words.txt','hello1.txt')----------------------------------------------------------------------AttributeErrorTraceback(mostrecentcalllast)in7fout.colse()8---->9sed('words.txt','hello1.txt')insed(file1,file2)5fout.write(fin)6#fin.close()---->7fout.colse()89sed('words.txt','hello1.txt')AttributeError:'_io.TextIOWrapper'objecthasnoattribute'colse'结果fout直接写入的文件格式也是_io.TextIOWrapper,无法关闭这成为一个难题。我觉得直接写一行字符串就可以关闭了。可能需要逐行读,逐行写。这要是能关了,还是有问题要解决的。如何关闭,需要再做测试fin=open('a.txt','r')print(fin)fin.close()<_io.TextIOWrappername='a.txt'mode='r'encoding='cp936'>没有报错,证明文件关闭成功。现在需要的是逐行读取文件,逐行写入defsed(file1,file2):fin=open(file1,'r')fout=open(file2,'w')forlineinfin:fout.write(line)fin.close()fout.close()sed('a.txt','e.txt')没有报错,去查看e.txt,发现也写了所有的内容a.txt到它。证明这一步是成功的,那么下一步就是处理替换问题了。尝试使用以前的格式运算符进行替换。我首先添加'%s'defsed(typstr,restr,file1,file2):fin=open(file1,'r')fout=open(file2,'w')forlineinfin:line=line%restrfout.write(line)fin.close()fout。close()sed('%s','zhao','a.txt','g.txt')----------------------------------------------------------------------TypeErrorTraceback(最近调用最后)in10#except:11#print('Somethingwentwrong')--->12sed('m','zhao','a.txt','g.txt')insed(typstr,restr,file1,file2)4fout=open(file2,'w')5forlineinfin:---->6line=line%restr7fout.write(line)8fin.close()TypeError:notallargumentsconvertedduringstringformatting这个错误说明前面%的参数个数和参数个数不对应后面查看文件a.txt,发现有的行加了'%s',有的行没有,这说明如果格式运算符和一行中的替换字符数如果不一样就会报错,所以我在每一行都加了一个'%s',然后就没有报错了。但是在这里我突然想到了一个问题。格式运算符的局限性在于不能替换其他内容,而且数字必须一致。如果文件内容很大,其实不好处理。于是我把函数的第一个参数改成了'm',想看看能不能换掉。我发现文件中的“%s”应该被替换。也就是说按照我现在的写法,sed函数第一个参数是无效的,因为有格式运算符,直接替换文件中的格式运算符。所以,我决定用之前学过的replace替换,然后加上try和except来捕获异常。defsed(typstr,restr,file1,file2):try:fin=open(file1,'r')fout=open(file2,'w')forlineinfin:line=line.replace(typstr,restr)#用restr替换typstrfout.write(line)fin.close()fout.close()except:print('Somethingwentwrong')sed('m','zhao','a.txt','g.txt')没有报错。查看g.txt后发现文件中的m已经被替换,而且没有数量限制。你完成了。我需要说一下捕获异常的用法。我其实刚开始写代码的时候就写了捕获异常的内容,直接报'Somethingwentwrong'。捕获异常是好事,但在实践中有点麻烦。你必须把这段代码去掉,然后再运行一下才能知道哪里出了问题,所以你不能在实践中使用这个。我想这个错误报告可能会让你在实际开发中代码很多的时候,找到哪里出了问题。当然,如果有可能无线递归或者循环报错的时候,在那个地方加上异常就很好了。否则,尽可能让自己暴露在错误中是一种更快的成长方式,而犯错证明你必须获得新的知识点。有一个文件处理遇到的管道问题,不过介绍的不多,以后有时间再学习。