重要说明

(1)python使用4个空格进行层次缩进的(不是tab),在eclipse里面可以直接使用tab缩进,是因为eclipse会实时地将tab转成4个空格

(2)在eclipse中安装pyDev插件,就可以调试python脚本了

(3)如果在python文件中存在中文字符,需要在python文件的开头处指明文件的编码类型,形式如:#coding=gbk

(4)以下代码使用的是python2.7.3版本

+++++++++  main.py  +++++++++

#coding=gbk
# filename : main.py
# author : kekec
# date : 20140813 import os,sys
import filedir
import filter file_suffix = '*.txt'
root_path = 'F:\\新建文件夹\\20140714'
result_path = unicode('result.txt' , "utf8")
wfile = open(result_path, 'w')
for i in filedir.search_file(file_suffix, root_path):
print i
bfile = False;
rfile = open(i, 'r')
while 1:
line = rfile.readline()
if not line:
break if (False == filter.is_filter(line)):
if (False == bfile):
wfile.write(i)
wfile.write('\n')
bfile = True
print line
wfile.write(line)
wfile.flush() rfile.close()
wfile.close()

+++++++++  filedir.py  +++++++++

#coding=gbk
# filename : filedir.py
# author : kekec
# date : 20140813 import os,sys,fnmatch def search_file(pattern="*.txt", root=os.curdir):
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)

+++++++++  filter.py  +++++++++

#coding=gbk
# filename : filter.py
# author : kekec
# date : 20140813 ALOG_0 = '[ERROR]'
BLOG_0 = 'OnGameUpdateDB'
CLOG_0 = 'Field25'
DLOG_0 = 'execute'
ELOG_0 = 'failed' def is_filter(line):
a = line.find(ALOG_0) >= 0
b = line.find(BLOG_0) >= 0
c = line.find(CLOG_0) >= 0
d = line.find(DLOG_0) >= 0
e = line.find(ELOG_0) >= 0 return (a and b and c and d and e)

使用python递归子目录处理日志文件的更多相关文章

  1. 使用python脚本实现统计日志文件中的ip访问次数

    使用python脚本实现统计日志文件中的ip访问次数,注意此脚本只适用ip在每行开头的日志文件,需要的朋友可以参考下 适用的日志格式: 106.45.185.214 - - [06/Aug/2014: ...

  2. Python中将打印输出导向日志文件

    a. 利用sys.stdout将print行导向到你定义的日志文件中,例如: import sys # make a copy of original stdout route stdout_back ...

  3. Linux系统——日志文件

    日志文件的分类 (1)内核及系统日志 由系统服务rsyslog管理,根据去主配置文件/etc/rsyslog.conf中的设置决定将内核消息及各种系统程序消息记录到什么位置. /etc/rsyslog ...

  4. docker容器中日志文件过大处理方法

    背景 :在日常工作中一个基于centos镜像构建起来的python爬虫程序,日志文件在两个月内到了500G,日志存放在根目录下面,在不扩容的情况下把这个问题给解决掉.通过定时任务和脚本的方法,定期的清 ...

  5. python递归列出目录及其子目录下所有文件

    python递归列出目录及其子目录下所有文件 一.前言 函数的递归,简单来说,就是函数内部调用自己 先举个小例子,求阶乘 def factorial(n): if n == 0: return 1 e ...

  6. C#递归遍历子目录与子目录中的文件

    [转载]作者:weixingstudio 采用C#,通过指定一个路径,来递归的遍历所有的子目录以及子目录中的文件,建一个类似资源管理器的目录树 先递归的遍历所有的子目录,如果没有子目录以后,则遍历所有 ...

  7. python 实时遍历日志文件

    首先尝试使用 python open 遍历一个大日志文件, 使用 readlines() 还是 readline() ? 总体上 readlines() 不慢于python 一次次调用 readlin ...

  8. Python同时向控制台和文件输出日志logging的方法 Python logging模块详解

    Python同时向控制台和文件输出日志logging的方法http://www.jb51.net/article/66756.htm 1 #-*- coding:utf-8 -*- 2 import ...

  9. Linux(9)后台运行python程序并输出到日志文件

    后台运行python程序并标准输出到文件 现在有test.py程序要后台部署, 里面有输出内容 使用命令: nohup python -u test.py > test.log 2>&am ...

随机推荐

  1. 2016年10月10日--穷举、迭代、while循环

    穷举 将所有可能性全部全部走一遍,使用IF筛选出满足的情况 练习: 1.单位给发了一张150元购物卡, 拿着到超市买三类洗化用品. 洗发水15元,香皂2元,牙刷5元. 求刚好花完150元,有多少种买法 ...

  2. BZOJ 2574: [Poi1999]Store-Keeper

    Description 推箱子. \(n,m\leqslant 100\) Sol Tarjan+边双连通分量+BFS. 直接搜索的复杂度是 \(n^6\) 记录人的位置,箱子的位置和转移. 箱子的位 ...

  3. Tomcat配置文件server.xml详解

    <?xml version='1.0' encoding='utf-8'?> <Server port="8005" shutdown="SHUTDOW ...

  4. Postman

    Postman功能(https://www.getpostman.com/features) 主要用于模拟网络请求包 快速创建请求 回放.管理请求 快速设置网络代理 安装 下载地址:https://w ...

  5. jQuery工作原理

    jQuery的开篇声明里有一段非常重要的话:jQuery是为了改变javascript的编码方式而设计的.从这段话可以看出jQuery本身并不是UI组件库或其他的一般AJAX类库.jQuery改变ja ...

  6. 【leetcode】Symmetric Tree

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  7. C#之系统自带保存属性

    源代码下载链接 程序开发很多时候需要根据运行环境做不通的参数配置,通过写ini之类的文本文件是一种方法,但这种方法也同时会把数据暴露 Winform开发中可以将需要配置的字段属性保存到程序中(其实也是 ...

  8. yum install 安装时报yum doesn't have enough cached data to continue.

    yum install 安装时报yum doesn't have enough cached data to continue. 安装epel,yum -y install epel-release后 ...

  9. Java for LeetCode 204 Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. 解题思路: 空间换时间,开一个空间 ...

  10. 【leetcode】Surrounded Regions(middle)☆

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...