首页 > 开发 > PHP > 正文

Python如何实现删除文件中含“指定内容”的行

2024-05-04 22:00:45
字体:大 中 小
来源:转载
供稿:网友

本文实例讲述了Python实现删除文件中含指定内容的行,分享给大家供大家参考,具体如下,代码如下:

  1. #!/bin/env python 
  2. importshutil, sys, os 
  3. darray=[ 
  4.   "Entering directory", 
  5.   "In function ", 
  6.   "Leaving directory", 
  7.   "__NR_SYSCALL_BASE", 
  8.   "arm-hisiv100-linux-ar ", 
  9.   "arm-hisiv100-linux-gcc ", 
  10.   "but argument is of type", 
  11.   "dereferencing type-punned pointer will break strict-aliasing rules", 
  12.   "differ in signedness", 
  13.   "does break strict-aliasing rules", 
  14.   "embedded '//0' in format", 
  15.   "excess elements in array initializer", 
  16.   "implicit declaration of", 
  17.   "make -C ", 
  18.   " rm -f", 
  19.   "this is the location of the previous definition", 
  20.   "warning: multi-line comment" 
  21. ] 
  22. defisInArray (array, line): 
  23.   foriteminarray: 
  24.     ifiteminline: 
  25.       returnTrue 
  26.   returnFalse 
  27. if__name__=='__main__': 
  28.   argv=sys.argv 
  29.   argc=len(argv) 
  30.   ifargc<2: 
  31.     print"Usage: %s"%(os.path.basename(argv[0])) 
  32.     exit() 
  33.   fname=argv[1] 
  34.   fresult=fname+".result" 
  35.   withopen(fname,'r') as f: 
  36.     withopen(fresult,'w') as g: 
  37.       forlineinf.readlines(): 
  38.         ifnotisInArray(darray, line): 
  39.           g.write(line) 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表