Python - 努力创建一个脚本,该脚本在所有子目录文件的文本中搜索字符串,然后打印到创建的文件

问题描述 投票:0回答:1

非常感谢一些帮助。我是一个脚本菜鸟,我已经被这个问题困扰了好几天。

我希望代码能做一些事情:

  1. 要求用户输入要搜索的字符串。
  2. 遍历给定文件路径的子目录。
  3. 打开具有所列扩展名类型之一的文件。
  4. 打开文件并搜索用户输入的字符串。
  5. 将查询结果打印到文本文件中。

代码似乎需要一些时间才能运行,但什么也没有出现。

导入操作系统路径

要求用户输入字符串进行搜索

search_str = input("关键字或词组: “)

存储文件名以供以后打印

文件名 = []

搜索路径

path = os.path.dirname(os.path.realpath(file))

可接受的文件扩展名

extensions = {".xlsx", ".txt", ".pdf", ".doc", ".docx", ".mb", ".xlsm", ".xltx", ".xltm"}

创建文件来存储搜索结果

search_files = open('search.txt', 'w') search_files.write(f'我在你的文件中搜索了“{search_str}”。 这是我发现的: ')

搜索文件关键字的程序

def search_all_files_by_keyword(路径):

# Store file count number
file_count = 0

for root, dirs, files in os.walk(path):

    for file in files:

        try:

            # Apply file type filter, search for acceptable ext in extension
            ext = os.path.splitext(file)
            if ext in extensions:

                # Define file pathway
                file_path = os.path.join(root, file)

                # Open file for reading
                with open(file, 'r') as f:

                    # Read file and search for keyword or phrase
                    if search_str in f.read():

                        # Add file path to file_names and increase file_count, then close file
                        file_names.append(file_path)
                        file_count += 1
                        f.close()

                    # If keyword or phrase is not found, do nothing and close file
                    else:
                        f.close()

        except:
            pass

# Print search results to file
if file_count >= 1:
    search_files.write(f"{file_names}\n")
else:
    search_files.write(f'No results found for "{search_str}".')

运行程序

search_all_files_by_keyword(路径)

python os.walk
1个回答
0
投票

尝试路径模块以这种方式搜索所有文件夹/子文件夹:


import re
from pathlib import Path

# Ask the user to enter string to search
search_str = input("Keyword or phrase:\n")

# Store file names for later printing
file_names = []

# Path to search
path = Path("path/to/directory") # Replace with your actual file path

# Acceptable file extensions
extensions = {".xlsx", ".txt", ".pdf", ".doc", ".docx", ".mb"]

# to search for string in a file
def search_in_file(file_path, search_str):
    with open(file_path, 'r', encoding='utf-8') as f:
        file_content = f.read()
        matches = re.findall(search_str, file_content)
        if matches:
            return matches
        else:
            return None

# Iterate through the sub-directories 
for file_path in path.glob("**/*"):
    if file_path.suffix in extensions:
        matches = search_in_file(file_path, search_str)
        
        if matches:
            file_names.append(str(file_path))
            
            with open("results.txt", "a", encoding='utf-8') as results_file:
                results_file.write(f"{file_path}\n")
                results_file.write(f"{matches}\n\n")
© www.soinside.com 2019 - 2024. All rights reserved.