首页 > 代码库 > python自定义查找文件内容
python自定义查找文件内容
#!/usr/bin/env python
#coding:utf8
#此脚本为查找递归目录下所有文件匹配的内容
import os,sys,tab
def paths(path):
list_path=os.walk(path)
all_file=[]
for p,d,fl in list_path:
for f in fl:
pfile=os.path.join(p,f)
if os.path.isdir(pfile):
paths(pfile)
all_file.append(pfile)
return all_file
def searchs(search,allfile,logfile):
list_search=open(logfile,"a+")
for i in allfile:
if os.path.isfile(i):
f = file(i)
c = file(i).readlines()
for l in range(len(c)):
k = f.readline()
if search in k:
list_search.write(i+"\n")
list_search.write(k)
list_search.close()
list_sea=file(logfile)
listc=‘\n‘.join(list_sea.readlines())
list_sea.close()
print len(listc)
return listc
if __name__ == "__main__":
log_file = "/root/cui/script/cui.log"
path_input = raw_input("\033[32;1mplease input search path: \033[0m").strip()
search_input = raw_input("\033[32;1mplease input the search: \033[0m").strip()
all_file = paths(path_input)
listc = searchs(search_input,all_file,log_file)
本文出自 “原创python发布区” 博客,请务必保留此出处http://cuihailong.blog.51cto.com/2923450/1409563