Skip to content

List #197

@TB941234

Description

@TB941234

import os

def find_files_by_keyword(root_directory, keyword):
"""
Searches for files containing a specific keyword in their name within a directory
and all its subdirectories.

Args:
    root_directory (str): The absolute path to the folder to start searching from.
    keyword (str): The word to search for in the filenames.
"""
found_files = []
print(f"Searching for files with '{keyword}' in the name, starting from '{root_directory}'...\n")

# os.walk() is a powerful generator that "walks" a directory tree
for dirpath, dirnames, filenames in os.walk(root_directory):
    for filename in filenames:
        # Check if the keyword (case-insensitive) is in the filename
        if keyword.lower() in filename.lower():
            full_path = os.path.join(dirpath, filename)
            found_files.append(full_path)

return found_files

--- HOW TO USE THIS SCRIPT ---

if name == "main":
# 1. SET THE FOLDER YOU WANT TO SEARCH
# Example for Windows: 'C:\Users\YourUsername\Documents'
# Example for macOS/Linux: '/Users/YourUsername/Documents'
# IMPORTANT: Use an absolute path for best results.
search_path = "C:\Users\YourUsername\Documents"

# 2. SET THE KEYWORD YOU ARE LOOKING FOR
keyword_to_find = "report" 

# Run the search function
results = find_files_by_keyword(search_path, keyword_to_find)

# 3. PRINT THE RESULTS
if results:
    print("--- Found Files ---")
    for file_path in results:
        print(file_path)
else:
    print(f"No files found with the keyword '{keyword_to_find}' in that location.")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions