Python实现删除文件中含“指定内容”的行示例
本文实例讲述了Python实现删除文件中含指定内容的行。分享给大家供大家参考,具体如下:
#!/bin/envpython importshutil,sys,os darray=[ "Enteringdirectory", "Infunction", "Leavingdirectory", "__NR_SYSCALL_BASE", "arm-hisiv100-linux-ar", "arm-hisiv100-linux-gcc", "butargumentisoftype", "dereferencingtype-punnedpointerwillbreakstrict-aliasingrules", "differinsignedness", "doesbreakstrict-aliasingrules", "embedded'\\0'informat", "excesselementsinarrayinitializer", "implicitdeclarationof", "make-C", "rm-f", "thisisthelocationofthepreviousdefinition", "warning:multi-linecomment" ] defisInArray(array,line): foriteminarray: ifiteminline: returnTrue returnFalse if__name__=='__main__': argv=sys.argv argc=len(argv) ifargc<2: print"Usage:%s"%(os.path.basename(argv[0])) exit() fname=argv[1] fresult=fname+".result" withopen(fname,'r')asf: withopen(fresult,'w')asg: forlineinf.readlines(): ifnotisInArray(darray,line): g.write(line)
另外还可参考:bash删除文件中含"指定内容"的行
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python文件与目录操作技巧汇总》、《Python文本文件操作技巧汇总》、《PythonURL操作技巧总结》、《Python图片操作技巧总结》、《Python数据结构与算法教程》、《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短