Python实现删除文件中含“指定内容”的行示例
作者:JoeBlackzqq 时间:2022-01-15 02:56:28
本文实例讲述了Python实现删除文件中含指定内容的行。分享给大家供大家参考,具体如下:
#!/bin/env python
import shutil, sys, os
darray = [
"Entering directory",
"In function ",
"Leaving directory",
"__NR_SYSCALL_BASE",
"arm-hisiv100-linux-ar ",
"arm-hisiv100-linux-gcc ",
"but argument is of type",
"dereferencing type-punned pointer will break strict-aliasing rules",
"differ in signedness",
"does break strict-aliasing rules",
"embedded '\\0' in format",
"excess elements in array initializer",
"implicit declaration of",
"make -C ",
" rm -f",
"this is the location of the previous definition",
"warning: multi-line comment"
]
def isInArray (array, line):
for item in array:
if item in line:
return True
return False
if __name__ == '__main__':
argv = sys.argv
argc = len(argv)
if argc < 2:
print "Usage: %s <file>" %(os.path.basename(argv[0]))
exit()
fname = argv[1]
fresult = fname + ".result"
with open(fname, 'r') as f:
with open(fresult, 'w') as g:
for line in f.readlines():
if not isInArray(darray, line):
g.write(line)
另外还可参考:bash删除文件中含"指定内容"的行
希望本文所述对大家Python程序设计有所帮助。
标签:Python,删除,文件
0
投稿
猜你喜欢
有时应该告诉我,但有时不应该告诉我
2009-03-19 13:40:00
php实现将数组或对象写入到文件的方法小结【三种方法】
2023-11-19 05:08:04
Python数据结构与算法之图的广度优先与深度优先搜索算法示例
2022-05-05 07:34:53
深入理解Vue生命周期、手动挂载及挂载子组件
2024-05-02 16:33:05
基于Django signals 信号作用及用法详解
2021-08-01 10:59:52
Oracle9i 动态SGA,PGA特性探索
2009-04-24 12:39:00
javascript分页代码实例分享(js分页)
2023-10-11 10:00:57
php实现utf-8和GB2312编码相互转换函数代码
2024-04-29 13:56:12
Python中Django与Echarts的结合用法图文详解
2022-02-09 23:10:29
PLSQL14下载与安装使用教程
2024-01-16 10:59:48
基于jupyter代码无法在pycharm中运行的解决方法
2023-09-15 06:46:38
用python画个敬业福字代码
2022-04-04 18:36:59
Python如何使用队列方式实现多线程爬虫
2022-03-24 08:56:51
JavaScript错误处理
2023-09-23 08:26:37
idea连接SQL Server数据库的详细图文教程
2024-01-14 05:32:54
Go实现一个配置包详解
2024-05-22 10:29:57
SQL SERVER 将XML变量转为JSON文本
2024-01-21 23:38:46
Python求解平方根的方法
2023-02-13 13:25:47
Python内置方法实现字符串的秘钥加解密(推荐)
2023-10-22 23:45:35
Python使用matplotlib.pyplot as plt绘图图层优先级问题
2022-06-10 18:00:37