用于在所有文件内容和子文件夹中搜索字符串的Python脚本

用于在所有文件内容和子文件夹中搜索字符串的Python脚本,python,string-search,Python,String Search,它给了我以下错误: import os from fnmatch import fnmatch root = 'Directory' for path, subdirs, file in os.walk(root): for files in file : with open(files) as f: contents = f.read() if 'Search_string' in contents: print os.path.join(path, files

它给了我以下错误:

import os
from fnmatch import fnmatch

root = 'Directory'


for path, subdirs, file in os.walk(root):
for files in file : 
with open(files) as f:
        contents = f.read()
    if 'Search_string' in contents:
print os.path.join(path, files)

请帮忙

Python语言完全依赖缩进。您在代码中没有给出任何缩进。因此,您应该在代码中给出缩进。您应该按照此链接进行缩进


这个错误是不言自明的。您应该缩进
for
循环中的行。
    for files in file :
      ^
IndentationError: expected an indented block
import os
from fnmatch import fnmatch

root = 'Directory'

for path, subdirs, file in os.walk(root):
    for files in file : 
        with open(files) as f:
            contents = f.read()
            if 'Search_string' in contents:
                print os.path.join(path, files)