# How to make arcpy to iterate through folders & sub-folders to access gdb's? [closed]

With reference to Check whether any feature classes in multiple gdb's has feature class representation, the code I've mentioned in the other thread is working great for single folder alone. We have 100's of gdb's in different folders & sub-folders.

Since I am very much new to arcpy, I don't know how to do this. I searched & found arcpy.da.walk has to be used to iterate through folders. I've tried that (code below). The code is getting processed but no results or not even throwing any errors. But the process gets completed every time.

Edit:

Based on the comments and code given in this link, i've changed the code

import os
import arcpy
def FindField(fc,myField):
fieldList = arcpy.ListFields(fc,myField)
for field in fieldList:
if str.lower(str(field.name)) == str.lower(myField):
print gdb, fc + " contains fieldname: " + myField
myField = "RuleID"

for path, dirs, files in os.walk(top_folder):
for d in dirs:
if not d.endswith(".gdb"):
continue
gdb_path = os.path.join(path, d)
arcpy.env.workspace = gdb_path

for fc in arcpy.ListFeatureClasses():
FindField(fc,myField)

for fds in arcpy.ListDatasets('','feature'):
for fc in arcpy.ListFeatureClasses('','',fds):
FindField(fc,myField)

• You will find it easier to implement new tools if you use the minimum amount of code to test and understand new functionality. This code does not need functions. If you strip it down to print statements, you'll have a better idea of what is happening. May 5 '16 at 13:17

I'd recommend the following because it's very simple, and it's also very worthwhile to get familiar with the os module.

import os
import arcpy

top_folder = r"path\to\top\folder"

for path, dirs, files in os.walk(top_folder):
for d in dirs:
if not d.endswith(".gdb"):
continue
gdb_path = os.path.join(path, d)
print gdb_path

arcpy.env.workspace = gdb_path
all_fcs = arcpy.ListFeatureClasses()
for fds in arcpy.ListDatasets('','feature'):
for fc in arcpy.ListFeatureClasses('','',fds):
all_fcs.append(fc)

for fc in all_fcs:
fieldnames = [f.name.lower() for f in arcpy.ListFields(fc)]
if myField.lower() in fieldnames:
print fc


Things I'm not 100% on (it's been a little while since I needed to use arcpy):

Matching field names by forcing to lower() may allow something to slip through the cracks that will cause trouble later on.

The whole feature class dataset iteration thing. You'll want to print everything out all the time to make sure you aren't hitting feature classes twice.

Ultimately, @Vince was right in the beginning: too much complexity for a simple task.

• I've used this code given here gis.stackexchange.com/questions/26892/… Passed gdb_path for arcpy.env.workspace. But the list is not getting iterated May 5 '16 at 14:49
• first, when gdb_path is printed, is it correct? second, which list are you talking about. I'll add a couple more lines to my answer that should get you close. May 5 '16 at 14:58
• I've edited the question and added the code. May 5 '16 at 15:16
• ok, at this point you're well beyond the scope of your original question, but I added a little bit more to my answer to accomplish what you need. The problem may have been that you were passing a wildcard to the listfields function (more specific) but later only testing after forcing the case (less specific). May 5 '16 at 15:33
• OK. I've just called the findfield function after your list.featureclass and list.fields. Now it's working perfectly. A big thanks. Very helpful May 5 '16 at 15:40