def search_in_path(path_original,str_targit):
targit_files = []
if (os.path.exists(path_original) and os.path.isabs(path_original)):
for root,dir,files in os.walk(path_original): #os.walk()遍历是个好东东
print root
print dir
print files
for file_obj in files:
file_whole_name = os.path.join(root,file_obj)
result = search_in_file(file_whole_name,str_targit)
if result !="":
targit_files.append(result)
else:
continue
else:
print "The path which you have input is not valid!"
print "最终包括这个内容的文件有:\n"
result_num = 0
for tar in targit_files:
print "%d\t%s" %(result_num,tar)
result_num = result_num+1
def search_in_file(file_original,str_targit):
if os.path.isfile(file_original):
fp = open(file_original,'r')
match = False
line = "file begin:"
i = 1
while line:
line = fp.readline()
#print "以下打印内容:\n" ,line
m = re.findall(str_targit,line)
#print "m = \t",m
if m !=[]:
match =True
print "在文件里找到了"
break
else:
continue
i = i+1
print "%_%"*28
print "match:\t",match
if match ==True:
print "Found it!\t"+file_original
fp.close()
print '*'*80
return file_original
else:
print "Nothing found!"
fp.close()
print '#'*80
return ""
def main():
search_in_path("C:\Python27", "xuqq")
if __name__ =='__main__':
main()作者: crossin先生 时间: 2016-2-16 22:35
查找文件里的内容不用readlines,直接read到一个字符串,然后find就可以了。
另外你这个代码好像无法检测子文件夹,因为你没有进一步对dir去递归搜索作者: morpheus2222 时间: 2017-1-22 09:03
import os
def find_certain_files(keyword = input("search:") ):
result = []
for path,dirs,files in os.walk("文件夹"):
for file in files:
if file.endswith(".txt"):
fileWithPath = os.path.join(path,file)
a = open(fileWithPath)
b = a.read()
if keyword in b:
result.append(fileWithPath)
a.close()
#!/usr/bin/python
#This file is used to search files in path A. The names of the files contains B, and the content of the files contain C
#the input should like this:python search.py A B C
import os
import re
import sys
def searchmethod(path,filetype,text):
all=[]
for dirpath, dirnames, filenames in os.walk(path):
for filename in filenames:
f=file(os.path.join(dirpath,filename))
content=f.read()
f.close()
#print content
if os.path.splitext(filename)[1]==filetype and re.findall(text, content): # to match file type and file content
all.append(os.path.join(dirpath,filename))
print all
if __name__=='__main__':
if len(sys.argv) !=4:
print 'Please input like this:./9.py your_dir file_type your_match_string'
elif os.path.exists(sys.argv[1]):
searchmethod(sys.argv[1],sys.argv[2],sys.argv[3]) 作者: 南斗 时间: 2017-3-27 22:53
#! /usr/bin/env python
#coding=utf-8
import os
rootdir = 'E:\Famine' #定义根目录
#三个参数:分别返回1.父目录 2.父目录下所有文件夹名字(不含路径) 3.父目录下所有文件名字
for father_path, foldernames, filenames in os.walk(rootdir):
for filename in filenames: #遍历文件
if os.path.splitext(filename)[1] == '.txt': #判断文件后缀是否是txt
def filterFile(filePath, contain):
for grandFather, father, sons in os.walk(filePath):
sonList = []
for son in sons:
sonList.append(son)
fnmatchs = fnmatch.filter(sonList, "*.txt")
if len(fnmatchs) != 0:
for fnmatchFile in fnmatchs:
absultePath = grandFather + "/" + fnmatchFile
absultePath = absultePath.replace("\\", "/")
f = file(absultePath)
str = f.read()
f.close()
if str.__contains__(contain):
print absultePath