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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
