【问题标题】:Python 2.7 Script - Searching for a String in all files in Directories and Sub-DirectoriesPython 2.7 脚本 - 在目录和子目录的所有文件中搜索字符串
【发布时间】:2018-05-05 11:00:46
【问题描述】:

我有一个名为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”的文本文件名称,但我需要它在文本文件中搜索字符串并输出包含该字符串的文件的路径方式。

我的问题是'如何获取代码以搜索文本文件中的(字符串)内容并打印“找到字符串 > 路径 C:/X/Y/Z?”

【问题讨论】:

    标签: python python-2.7 search operating-system os.walk


    【解决方案1】:

    至少对我来说 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-03
      • 2012-05-22
      • 2014-10-16
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      相关资源
      最近更新 更多