Using a search cursor for a list?

2507
8
04-30-2012 09:37 AM
StevenWorkman
New Contributor III
Hello all.  Im a novice when it comes to python.  Im trying to use a search cursor to retrieve the specific value "drillhole" in the "Label" field of the attribute table for each feature class in a large directory.  Here is my code:

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 gdb:
        arcpy.env.workspace = fc
        fcl = arcpy.ListDatasets("*", "Feature")
        for fcc in fcl:
            arcpy.env.workspace = fcc
            fccl = arcpy.ListFeatureClasses("*", "All")
            print fccl


Is there a way to use the search cursor on the list "fccl" in order to loop through all of these feature datasets and return the value "drillhole"?  Thanks for any help.
Tags (2)
0 Kudos
8 Replies
RyanForbes1
New Contributor III
Certainly!  You would need to go one for loop further.

for item in fccl:
.....

Then from there you would create the cursor, nest in a while loop, and then do what you need to do.  Just be sure to delete the cursor at the end of the loop, that way it creates it and destroys it all within the loop and nothing escapes once its done.
0 Kudos
MathewCoyle
Frequent Contributor
Not quite sure what you are asking. What about the attribute 'drillhole' in the 'Label' field do you want to know? The OID? Number of occurrences? Here's an example of identifying the row and then you can do what you want from there.

field = "Label"
value = "drillhole"
for fc in fccl:
    try:
        s_curs = arcpy.SearchCursor(fc,"%s = '%s'" % (field,value),"","Label")
        for row in s_curs:
            # do something as each row should be a drillhole
0 Kudos
DarrenWiens2
MVP Honored Contributor
Untested, but I think it should work. You'll have to change your workspace environment to point at the individual feature class, also.
...continue indentation from previous part of script...
    fccl = arcpy.ListFeatureClasses("*", "All")
    for featureclass in fccl:
        rows = arcpy.SearchCursor(featureclass)
        for row in rows:
             drillhole = row.Label
0 Kudos
StevenWorkman
New Contributor III
Not quite sure what you are asking. What about the attribute 'drillhole' in the 'Label' field do you want to know? The OID? Number of occurrences? Here's an example of identifying the row and then you can do what you want from there.

field = "Label"
value = "drillhole"
for fc in fccl:
    try:
        s_curs = arcpy.SearchCursor(fc,"%s = '%s'" % (field,value),"","Label")
        for row in s_curs:
            # do something as each row should be a drillhole


Thanks for you help Mathew. I am trying to find out which feature classes in a directory with 200+ folders has drillholes.   I tried the code you gave me but this was what was returned:

General function failure [ballinp_1]
Too few parameters. Expected 1.
0 Kudos
MathewCoyle
Frequent Contributor
Thanks for you help Mathew. I am trying to find out which feature classes in a directory with 200+ folders has drillholes.   I tried the code you gave me but this was what was returned:

General function failure [ballinp_1]
Too few parameters. Expected 1.


That would probably be because there was no except statement added at the end. A simpler way to go about it might be to make a temp feature layer from each fc and do a select by attribute to see if get count returns anything.
0 Kudos
StevenWorkman
New Contributor III
That would probably be because there was no except statement added at the end. A simpler way to go about it might be to make a temp feature layer from each fc and do a select by attribute to see if get count returns anything.


Ok. Ill try that method. You are saying that I need use MakeFeatureLayer_management (in_features, out_layer, where_clause, workspace, field_info) and make my in_features the list of layers (fccl). Does that need to be nested inside the loop? If you could, can you help me out with the code for that? Im a recent grad and am really new to this stuff. Thanks for any help. Heres what I have so far, but it isnt working.

for w in ws:
    arcpy.env.workspace = w 
    gdb = arcpy.ListWorkspaces("*", "Access")
    for fc in gdb:
        arcpy.env.workspace = fc
        fcl = arcpy.ListDatasets("*", "Feature")
        for fcc in fcl:
            arcpy.env.workspace = fcc
            fccl = arcpy.ListFeatureClasses("*", "All")
            for stil in fccl:
                arcpy.env.workspace = fccl
                stil = arcpy.SaveToLayerFile_management("fccl","fccl2","")


Traceback (most recent call last):
File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 322, in RunScript
debugger.run(codeObject, __main__.__dict__, start_stepping=0)
File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\__init__.py", line 60, in run
_GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
File "C:\Python26\ArcGIS10.0\Lib\site-packages\Pythonwin\pywin\debugger\debugger.py", line 655, in run
exec cmd in globals, locals
File "C:\Documents and Settings\WorkmanS\Desktop\Script5.py", line 16, in <module>
arcpy.env.workspace = fccl
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 505, in set_
self[env] = val
File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py", line 557, in __setitem__
ret_ = setattr(self._gp, item, value)
RuntimeError: <unprintable RuntimeError object>
0 Kudos
MathewCoyle
Frequent Contributor
Close. You can't make a feature class a workspace though. And you just want a temp layer for selection not a permanent layer file.

Try something like this.

import arcpy
arcpy.env.overwriteOutput = True
tempLayer = r"in_memory\templayer"
field = "Label"
value = "drillhole"
where = "%s = '%s'" % (field,value)
dhList = []
for w in ws:
    arcpy.env.workspace = w
    gdb = arcpy.ListWorkspaces("*", "Access")
    for fc in gdb:
        arcpy.env.workspace = fc
        fcl = arcpy.ListDatasets("*", "Feature")
        for fcc in fcl:
            arcpy.env.workspace = fcc
            fccl = arcpy.ListFeatureClasses("*", "All")
            for stil in fccl:
                arcpy.MakeFeatureLayer_management(stil, tempLayer, where)
                arcpy.SelectLayerByAttribute_management(tempLayer)
                count = int(arcpy.GetCount_management(tempLayer).getOutput)
                if count > 0:
                    dhList.append(stil)
for item in dhList:
    print item
0 Kudos
StevenWorkman
New Contributor III
I made a few adjustments to the script and it runs without getting any errors. I highlighted the changes in red:

import arcpy
arcpy.env.workspace = "X:"
ws = arcpy.ListWorkspaces("*","Folder")
arcpy.env.overwriteOutput = True
tempLayer = r"in_memory\templayer"
field = "Label"
value = "drillholes"
where = "%s = '%s'" % (field,value)
dhList = []
for w in ws:
    arcpy.env.workspace = w
    gdb = arcpy.ListWorkspaces("*", "Access")
    for fc in gdb:
        arcpy.env.workspace = fc
        fcl = arcpy.ListDatasets("*", "Feature")
        for fcc in fcl:
            arcpy.env.workspace = fcc
            fccl = arcpy.ListFeatureClasses("*", "All")
            for stil in fccl:
                arcpy.MakeFeatureLayer_management(stil, tempLayer, where)
                arcpy.SelectLayerByAttribute_management(tempLayer,"SUBSET_SELECTION")
                count = str(arcpy.GetCount_management(tempLayer).getOutput)
                if count > 0:
                    dhList.append(stil)
for item in dhList:
    print item



The only problem now is the output is showing all of the feature classes in my directory instead only the feature classes that contain drillholes.
0 Kudos