need help with looping through .mdb, feature datasets, feature classes.

2400
3
04-27-2012 05:27 AM
StevenWorkman
New Contributor III
Ok, so basically Im trying to loop through all of my folders, mdbs, feature datasets, and feature classes in order use the SearchCursor to read values in the feature classes attribute table.  I am running this code and it is only returning the last feature dataset in the list.  It is returning the list of folders, mdbs, but only the very last feature dataset and feature class.  What do I need to change in my code so that it returns all of the feature datasets and feature classes?  Any help is appreciated.


import arcpy
arcpy.env.workspace = "X:"
ws = arcpy.ListWorkspaces("*","Folder")

for w in ws:
    arcpy.env.workspace = w 
    gdb = arcpy.ListWorkspaces("*", "Access")
    

for fc in arcpy.ListWorkspaces("*", "Access"):
    arcpy.env.workspace = fc
    fcl = arcpy.ListDatasets("*", "Feature")

for fcc in arcpy.ListDatasets("*", "Feature"):
    arcpy.env.workspace = fcc
    fccl = arcpy.ListFeatureClasses("*", "All")
    print fccl
    


Here is the output:

[u'X:\\St_Stephens', u'X:\\Summerton_GQM6', u'X:\\SummervilleNW', u'X:\\Sunset_GQM28', u'X:\\Swansea_OFR40', u'X:\\Table_Rock_GQM9', u'X:\\Taxahaw_OFR5', u'X:\\Taylors_OFR133&134', u'X:\\Tigerville_GQM45', u'X:\\Tillman', u'X:\\Trenton', u'X:\\Tugaloo_Lake_OFR156', u'X:\\Tybee', u'X:\\Unity', u'X:\\Valley_Falls_OFR96', u'X:\\Vance', u'X:\\Van_Wyck', u'X:\\Wagener', u'X:\\Ware_Shoals_East_GQM12', u'X:\\Ware_Shoals_West_GQM26', u'X:\\Wateree_GQM36', u'X:\\Waterloo_OFR82', u'X:\\Waxhaw', u'X:\\Wedgefield_GQM47', u'X:\\Wellford_OFR95', u'X:\\Whetstone_OFR155', u'X:\\Wiggins_GQM18', u'X:\\Williston_OFR75', u'X:\\Windsor_OFR90', u'X:\\Woodruff_OFR121', u'X:\\Zirconia_GQM39']


[u'X:\\St_Stephens\\stste.mdb']
[u'X:\\Summerton_GQM6\\summe.mdb']
[u'X:\\SummervilleNW\\sumnw.mdb']
[u'X:\\Sunset_GQM28\\sunse.mdb']
[u'X:\\Swansea_OFR40\\swans.mdb']
[u'X:\\Table_Rock_GQM9\\tablr.mdb']
[u'X:\\Taxahaw_OFR5\\taxah.mdb']
[u'X:\\Taylors_OFR133&134\\taylo.mdb']
[u'X:\\Tigerville_GQM45\\tiger.mdb']
[u'X:\\Tillman\\tillm.mdb']
[u'X:\\Trenton\\trent.mdb']
[u'X:\\Tugaloo_Lake_OFR156\\tugal.mdb']
[u'X:\\Tybee\\tyino.mdb']
[u'X:\\Unity\\Unity.mdb']
[u'X:\\Valley_Falls_OFR96\\vallf.mdb']
[u'X:\\Vance\\vance.mdb']
[u'X:\\Van_Wyck\\VanWyck.mdb']
[u'X:\\Wagener\\wagen.mdb']
[u'X:\\Ware_Shoals_East_GQM12\\wasea.mdb']
[u'X:\\Ware_Shoals_West_GQM26\\waswe.mdb']
[u'X:\\Wateree_GQM36\\watee.mdb']
[u'X:\\Waterloo_OFR82\\watlo.mdb']
[u'X:\\Waxhaw\\Waxhaw.mdb']
[u'X:\\Wedgefield_GQM47\\wedge.mdb']
[u'X:\\Wellford_OFR95\\wellf.mdb']
[u'X:\\Whetstone_OFR155\\whets.mdb']
[u'X:\\Wiggins_GQM18\\wiggi.mdb']
[u'X:\\Williston_OFR75\\wills.mdb']
[u'X:\\Windsor_OFR90\\winds.mdb']
[u'X:\\Woodruff_OFR121\\woodr.mdb']
[u'X:\\Zirconia_GQM39\\zirco.mdb']
[u'zirco']
[u'zircoa_1', u'zircop_1', u'zircol_1']
Tags (2)
0 Kudos
3 Replies
CoryMacNeil1
Occasional Contributor II
Hi Steven,

Your main problem is your indentation in the Python code.  Your loops should be nested.  See a similar forum posting here.

I hope this helps.
Cory
0 Kudos
curtvprice
MVP Esteemed Contributor
Ok, so basically Im trying to loop through all of my folders, mdbs, feature datasets, and feature classes in order use the SearchCursor to read values in the feature classes attribute table. 


There's a nice example in the online help here (bottom of the article) that shows the indentation and setting the workspaces correctly. If you started with this example it may be worth another look.

Arc 10.0 help: Listing data

One thing I see you're doing is listing things more than once, the list functions return a list which you can then iterate over, your code seems to try to generate another list instead of using the one you just created.... here's a suggested approach:

for w in ws:
    arcpy.env.workspace = w 
    gs = arcpy.ListWorkspaces("*", "Access")
    for g in gs:        
        arcpy.env.workspace = g
        fds = arcpy.ListDatasets("*", "Feature")
        for fd in fds ...    
0 Kudos
StevenWorkman
New Contributor III
Really appreciate the help you guys! Ill check back if I cant get this thing going.
0 Kudos