我正在尝试使这个函数(在dir中搜索给定的字符串)搜索所有子目录,并以递归方式执行此操作。我不太清楚Python的开头。任何指导都会很棒。
谢谢!
def grep(regex, base_dir):
matches = list()
for filename in os.listdir(base_dir):
full_filename = os.path.join(base_dir, filename)
if not os.path.isfile(full_filename):
continue
with open(os.path.join(base_dir, filename)) as fh:
content = fh.read()
matches = matches + re.findall(regex, content)
return matches
答案 0 :(得分:2)
如果您要抓取整个目录,请尝试os.walk()
。这样的东西可能会起作用(未经测试,但如果不起作用则可以调整):
def grep(regex, base_dir):
matches = list()
# os.walk() returns a tuple - the directory path, a list of directories and the files
for dirpath, dirname, files in os.walk(base_dir):
# Iterate through the directory list, reading the files
for directory in dirname:
for filename in os.listdir(directory):
with open(os.path.join(base_dir, directory, filename)) as fh:
content = fh.read()
matches = matches + re.findall(regex, content)
return matches
答案 1 :(得分:1)
对于递归遍历,请尝试os.walk
。您可以在这里找到如何使用它:www.saltycrane.com/blog/2007/03/python-oswalk-example /
答案 2 :(得分:1)
我会用这样的东西:
def find_file_matches(filename, regex):
with open(filename, 'rt') as fh:
return re.findall(regex, fh.read())
def walktree(top):
""" Walk the directory tree starting from top, and
yield a tuple of each folder and all the files in it. """
names = os.listdir(top)
yield top, (name for name in names if not os.path.isdir(name))
for name in names:
if os.path.isdir(name):
for (newtop, children) in walktree(os.path.join(top, name)):
yield newtop, children
def grep(regex, base_dir="."):
matches = []
for dir, files in walktree(base_dir):
for file in files:
filename = os.path.join(dir, file)
matches.append(find_file_matches(filename, regex))
return matches
答案 3 :(得分:-1)
从命令行
find . -type d | grep -i nameofdir
或类似的东西。