Python处理日志之取得文件列表(更新)

这篇文章里面讲述了如何通过glob模块取得列表,但由于glob能使用的正则表达式有限,所以只能说是完成了部分任务,今天看《Python技术参考大全》受到启发,我们可以使用正则表达式去检查每个文件名,这样就可以找到所需的文件列表。

fileList = []

pattern = r"seeUthere_errors.log(\.\d{4}-\d{2}-\d{2}-\d{2})"


for eachfile in glob.glob(r"D:\Log\./*"):
if re.search(pattern, eachfile):
fileList.append(eachfile)


延伸:

如果要取得当天生成的复合正则表达式的log 列表呢?

按照日志生成的格式,我们可以利用time模块的strftime方法,得到当前天:2009-11-12

import time

current = time.strftime("%Y-%m-%d", time.gmtime())

然后修改pattern:

pattern = r"seeUthere_errors.log(\.)" + current + "(-\d{2})"

再采取上面的方法就可以了。

posted @ 2009-11-12 14:57  小楼  阅读(886)  评论(0编辑  收藏  举报