You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:编写Python脚本遍历目录检测Test子目录状态

Solution for Checking Empty Test Subdirectories

Got it, here's a straightforward Python script that meets your requirement, plus some extra tips to handle edge cases:

The Core Script

import os

# Define the root directory to traverse
root_dir = r"E:\abc"

# Recursively walk through all directories under the root
for dirpath, _, _ in os.walk(root_dir):
    # Construct the path to the Test subdirectory in current directory
    test_subdir = os.path.join(dirpath, "Test")
    
    # Check if the Test subdirectory exists
    if os.path.isdir(test_subdir):
        # Determine if the directory is empty
        if not os.listdir(test_subdir):
            print(f"OK: {test_subdir} is empty")
        else:
            print(f"NOT_OK: {test_subdir} is not empty")

How It Works

  • os.walk(): This function recursively traverses every directory under E:\abc. We use dirpath (the current directory's full path) and ignore the other two returned values (dirnames and filenames) with underscores.
  • os.path.join(): Safely constructs the path to the Test subdirectory, avoiding issues with Windows path separators.
  • os.path.isdir(): Confirms that Test is actually an existing directory (not a file with the same name).
  • os.listdir(): Lists all items in the Test directory. If the returned list is empty, the directory has no contents.

Optional Enhancements

1. Ignore Hidden Files/Directories

If you want to treat directories with only hidden items as "empty" (common on Windows where system hidden files might exist), modify the check:

import os

root_dir = r"E:\abc"

for dirpath, _, _ in os.walk(root_dir):
    test_subdir = os.path.join(dirpath, "Test")
    if os.path.isdir(test_subdir):
        # Filter out hidden items (names starting with '.' or system-hidden)
        visible_items = [item for item in os.listdir(test_subdir) 
                        if not item.startswith('.') 
                        and not (os.stat(os.path.join(test_subdir, item)).st_file_attributes & 0x02)]
        
        if not visible_items:
            print(f"OK: {test_subdir} has no visible content")
        else:
            print(f"NOT_OK: {test_subdir} contains visible items")

2. Handle Permission Errors

If some directories are restricted, add error handling to avoid script crashes:

import os

root_dir = r"E:\abc"

for dirpath, _, _ in os.walk(root_dir):
    test_subdir = os.path.join(dirpath, "Test")
    try:
        if os.path.isdir(test_subdir):
            if not os.listdir(test_subdir):
                print(f"OK: {test_subdir} is empty")
            else:
                print(f"NOT_OK: {test_subdir} is not empty")
    except PermissionError:
        print(f"⚠️ Permission denied: Cannot access {test_subdir}")
    except Exception as e:
        print(f"❌ Unexpected error with {test_subdir}: {str(e)}")

内容的提问来源于stack exchange,提问作者winx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 09:17:33