How to Quickly Detect Keyworded Folders in Python: A Simple Script

This article demonstrates a straightforward Python script that walks through a directory tree and prints folders whose names contain specific keywords such as "分公司" or "营销中心", providing a practical solution for automated file‑system checks.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
How to Quickly Detect Keyworded Folders in Python: A Simple Script

1. Introduction

The author received a question in a Python community about how to determine whether folder names under multiple directories contain the keywords “分公司” (branch office) or “营销中心” (marketing center). The article presents a concise Python solution.

2. Implementation

The following script uses os.walk to traverse the directory tree, checks each folder name against a list of keywords, and prints a message when a match is found.

import os

def check_folders(root_directory, keywords):
    for root, dirs, files in os.walk(root_directory):
        for dir_name in dirs:
            for keyword in keywords:
                if keyword in dir_name:
                    print(f"Folder '{dir_name}' contains the keyword '{keyword}'.")

root_directory = "path/to/your/root/directory"  # replace with your actual path
keywords = ["分公司", "营销中心"]

check_folders(root_directory, keywords)

This script successfully solves the problem by listing all matching folders.

3. Conclusion

The article provides a clear example of using Python for automation tasks related to file system management, helping readers implement a quick check for specific keywords in folder names.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonscriptfile-systemdirectory-search
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.