<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SearchCursor directory and subdirectories using python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/searchcursor-directory-and-subdirectories-using/m-p/197901#M15167</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;I work with arcmap 10.3 and python 2.7.8. I have more than 500 shapefiles located in many folders and subFolders. All Sub Folders are located in one large directory. &lt;/SPAN&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;I try with arcpy to detect all&amp;nbsp; &lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;shapefiles &lt;/SPAN&gt;that have in their attribute table ,in field name "YEUD", the value 20. I search all shape files that begin with letters "mig". Finally i tried to print all the shapefiles that had been found with value 20 in it. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;When i run this code i get en error:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os,sys,string,fnmatch
import arcpy.mapping
from arcpy import env

rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
fields = arcpy.ListFields(rootPath)
for root, dirs, files in os.walk(rootPath):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in fnmatch.filter(files, pattern):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print( os.path.join(root, filename))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(rootPath, field.name) as rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name == 'YEUD':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row == '20':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print( os.path.join(root, filename))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;RuntimeError: ERROR 999999: Error executing function.
&amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;P&gt;I can't understand why it doesn't work ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 09:53:12 GMT</pubDate>
    <dc:creator>Yaron_YosefCohen</dc:creator>
    <dc:date>2021-12-11T09:53:12Z</dc:date>
    <item>
      <title>SearchCursor directory and subdirectories using python</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-directory-and-subdirectories-using/m-p/197901#M15167</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;I work with arcmap 10.3 and python 2.7.8. I have more than 500 shapefiles located in many folders and subFolders. All Sub Folders are located in one large directory. &lt;/SPAN&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;I try with arcpy to detect all&amp;nbsp; &lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;shapefiles &lt;/SPAN&gt;that have in their attribute table ,in field name "YEUD", the value 20. I search all shape files that begin with letters "mig". Finally i tried to print all the shapefiles that had been found with value 20 in it. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #333333; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;When i run this code i get en error:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy,os,sys,string,fnmatch
import arcpy.mapping
from arcpy import env

rootPath = r"C:\Project\layers"
pattern = 'mig*.shp'
fields = arcpy.ListFields(rootPath)
for root, dirs, files in os.walk(rootPath):
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in fnmatch.filter(files, pattern):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print( os.path.join(root, filename))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(rootPath, field.name) as rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if field.name == 'YEUD':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row == '20':
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print( os.path.join(root, filename))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;RuntimeError: ERROR 999999: Error executing function.
&amp;gt;&amp;gt;&amp;gt; &lt;/PRE&gt;&lt;P&gt;I can't understand why it doesn't work ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:53:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-directory-and-subdirectories-using/m-p/197901#M15167</guid>
      <dc:creator>Yaron_YosefCohen</dc:creator>
      <dc:date>2021-12-11T09:53:12Z</dc:date>
    </item>
    <item>
      <title>Re: SearchCursor directory and subdirectories using python</title>
      <link>https://community.esri.com/t5/python-questions/searchcursor-directory-and-subdirectories-using/m-p/197902#M15168</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here's a revised script for you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import os
import fnmatch
import arcpy
rootPath = r"C:\Project\layers" 
pattern = 'mig*.shp' 
&lt;SPAN style="font-size: 9pt; line-height: 12pt;"&gt;for root, dirs, files in os.walk(rootPath): &lt;/SPAN&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename in fnmatch.filter(files, pattern): 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; shp = os.path.join(root, filename)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if arcpy.ListFields(shp, "YEUD"):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("{} has YEUD field".format(shp)) 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; with arcpy.da.SearchCursor(shp, ["YEUD"]) as rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if row[0] == 20:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print("{} has a record with YEUD = 20".format(shp))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; break&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;A few notes:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;Line 1: I'm adopting the Python convention &lt;A href="https://www.python.org/dev/peps/pep-0008/#imports" rel="nofollow noopener noreferrer" target="_blank"&gt;(PEP-8)&lt;/A&gt; of putting imports on their own line (easier to edit out modules you don't need that way).&lt;/LI&gt;&lt;LI&gt;Line 9: You were running &lt;STRONG&gt;arcpy.ListFields&lt;/STRONG&gt; on a folder at the beginning. You need to run it on a dataset, inside the loop.&lt;/LI&gt;&lt;LI&gt;Line 9: The approach I normally use to test for field existence is to call &lt;STRONG&gt;arcpy.ListFields(tbl, field_name)&lt;/STRONG&gt;, if it comes back as an empty list&amp;nbsp; &lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;[]&lt;/STRONG&gt;&lt;/SPAN&gt;, this means the field was not there. An empty list is interpreted as &lt;SPAN style="font-family: 'courier new', courier;"&gt;&lt;STRONG&gt;False&lt;/STRONG&gt;&lt;/SPAN&gt; when used in an if statement.&lt;/LI&gt;&lt;LI&gt;Line 11: The cursor is run here on the shape file table, not rootPath.&lt;/LI&gt;&lt;LI&gt;Line 15: Once I find a row with YEUD = 20, I jump out of the loop so I don't report more than once if there are multiple rows with that value.&lt;/LI&gt;&lt;LI&gt;ArcGIS 10.1 SP1 and later includes a similar iterator function &lt;STRONG&gt;arcpy.da.walk&lt;/STRONG&gt;, which can navigate both folder and geodatabase workspaces (not just file system folders as &lt;STRONG&gt;os.walk&lt;/STRONG&gt; does).&lt;/LI&gt;&lt;/UL&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:53:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/searchcursor-directory-and-subdirectories-using/m-p/197902#M15168</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2021-12-11T09:53:15Z</dc:date>
    </item>
  </channel>
</rss>

