import os def search_files(directory, keyword): for filename in os.listdir(directory): full_path = os.path.join(directory, filename) if os.path.isfile(full_path): with open(full_path, 'r', encoding='utf-8', errors='ignore') as f: if keyword in f.read(): print(f"在文件 {filename} 中找到关键词: {keyword}") # 堆代码 duidaima.com # 示例用法 search_files("documents_folder", "Python")该脚本适合在多个文档或代码文件中查找特定的关键词,并输出包含关键词的文件名。