Checking folder within a folder

05-13-2022 05:44 PM
New Contributor


I need to check to see if there are any folders that exist ( empty or with data) within another folder. I am trying to create a custom tool that will check that. 

so if there is any folder within a folder, the tool will display some message. 

Any help on creating such a python script will be very helpful: ) thanks!


0 Kudos
3 Replies
MVP Esteemed Contributor

python's os.path, pathlib, glob and arcpy's da.Walk

all have means to traverse a folder structure and return various bits of info (eg list of folders and their files etc)

have a look and see what suits your particular needs

... sort of retired...
0 Kudos
New Contributor

Hi Dan, 

Thank you a lot. I know da.walk can walk top to bottom, but i think my problem is about writing the actual code. like how to do check simply if there is a file folder within a file folder ?

0 Kudos
MVP Regular Contributor

Using the os module:


import os

root_dir = r"C:\yourFolderNameHere"
subdir = [
    i for i in os.listdir(root_dir)
    if os.path.isdir(os.path.join(root_dir, i))
if subdir:
    # handle having subdirectories


This will create a Python list with the name of all folders that exist within your root_directory folder. If there are no folders, the result will be an empty list, which is falsy when evaluated with an if statement.