Python 2.7 脚本 - 在目录和子目录的所有文件中搜索字符串

Python 2.7 Script - Searching for a String in all files in Directories and Sub-Directories

我有一个名为 documents 的文件夹,其中有 3,000 个文本文件和两个子目录:其中包含数千个文本文件。

我正在尝试对其进行编码,以便它搜索目录和子目录中的内容。

例如: 我希望 python 脚本在所有文本文件中搜索字符串,如果找到,则输出路径文本文件名和字符串。

目前我得到的代码是:

import os
import glob

os.chdir("C:\Users\Dawn Philip\Documents\documents")

for files in glob.glob( "*.txt" ):
f = open( files, 'r' )
file_contents = f.read()
if "x" in file_contents:
    print f.name

当我 运行 这个时,它显示了所有包含 "x" 的文本文件名,但我需要它在文本文件中搜索字符串并输出路径包含字符串的文件。

我的问题是“如何获取代码以在文本文件中搜索(字符串)内容并打印 "String Found > Path C:/X/Y/Z?"

至少对我来说 glob.glob() 只搜索了顶级目录。

import os
import glob

# Sets the main directory
main_path = "C:\Users\Dawn Philip\Documents\documents"

# Gets a list of everything in the main directory including folders
main_directory = os.listdir(main_path)

# This list will hold all of the folders to search through, including the main folder
sub_directories = []

# Adds the main folder to to the list of folders
sub_directories.append(main_path)

# Loops through everthing in the main folder, searching for sub folders
for item in main_directory:
    # Creates the full path to each item)
    item_path = os.path.join(main_path, item)

    # Checks each item to see if it is a directory
    if os.path.isdir(item_path) == True:
        # If it is a folder it is added to the list
        sub_directories.append(item_path)

for directory in sub_directories:
    for files in glob.glob(os.path.join(directory,"*.txt")):
        f = open( files, 'r' )
        file_contents = f.read()
        if "x" in file_contents:
            print f.name