<?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 Re: iterating through a shapefile in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221275#M17103</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Check out &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgiSDEsktop/9.3/index.cfm?TopicName=Data_access_using_cursors"&gt;cursors&lt;/A&gt;&lt;SPAN&gt;. Also, &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Reading_geometries"&gt;reading &lt;/A&gt;&lt;SPAN&gt;and &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgisdesktop/9.3/body.cfm?id=964&amp;amp;pid=951&amp;amp;topicname=Writing%20geometries&amp;amp;tocVisable=0"&gt;writing &lt;/A&gt;&lt;SPAN&gt;geometries.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 04 Apr 2012 19:11:34 GMT</pubDate>
    <dc:creator>DarrenWiens2</dc:creator>
    <dc:date>2012-04-04T19:11:34Z</dc:date>
    <item>
      <title>iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221274#M17102</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm new to geoprocessing with Python (but with a basic knowledge of Python) and have been trying to loop through a shapefile to perform analysis on each polygon in the shapefile.&amp;nbsp; I have seen references to looping through directories. etc. but haven't been able to translate them to working with polygons within a shapefile.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Mike&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using ArcGIS 9.3 on XP.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Apr 2012 19:01:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221274#M17102</guid>
      <dc:creator>MikeKnowles</dc:creator>
      <dc:date>2012-04-04T19:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221275#M17103</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Check out &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgiSDEsktop/9.3/index.cfm?TopicName=Data_access_using_cursors"&gt;cursors&lt;/A&gt;&lt;SPAN&gt;. Also, &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Reading_geometries"&gt;reading &lt;/A&gt;&lt;SPAN&gt;and &lt;/SPAN&gt;&lt;A href="http://webhelp.esri.com/arcgisdesktop/9.3/body.cfm?id=964&amp;amp;pid=951&amp;amp;topicname=Writing%20geometries&amp;amp;tocVisable=0"&gt;writing &lt;/A&gt;&lt;SPAN&gt;geometries.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Apr 2012 19:11:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221275#M17103</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-04-04T19:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221276#M17104</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi Mike,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Well if you're new to Python and arcpy it may seem confusing but I'll give you some code to work with. Shapefiles are not just a single file, so you need to use the arcpy module to run some of the commands. Here's a basic program that takes all the shapefiles in a directory and saves them to a list, it then iterates over every shapefile in the directory doing a print statement to the geoprocessor. Of course you could modify the print statement to do any type of geoprocessing. I haven't tried this code, but in theory it should work.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;
# Import your modules, arcpy is assuming you are running ArcGIS 10x
import arcpy, sys, os, fileinput, glob, string
from arcpy import env

# Get a directory as an argument from the user. This directory is where your shapefiles are
workingDirectory = sys.argv[1]

# This sets your workspace as the user defined working directory
env.workspace = WorkingDirectory

#This establishes the variable featureClass as a list of shapefiles using the arcpy command ListFeatureClasses
featureClass = arcpy.ListFeatureClasses()

# For loop to iterate over every shapefile in the workingDirectory that is referenced as the variable featureClass
for shapefile in featureClass:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(shapefile)

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:47:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221276#M17104</guid>
      <dc:creator>Jasonvan_Warmerdam</dc:creator>
      <dc:date>2021-12-11T10:47:14Z</dc:date>
    </item>
    <item>
      <title>Re: iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221277#M17105</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've written the following code based on "Writing Geoprocessing Scripts with ArcGIS" but am getting an error which I can't figure out. What is Python storing the data as (string, list, etc)? Has there been changes to how ArcGIS interprets the code?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using Python 2.5.1; ArcGIS 9.3; Windows XP.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcgisscripting&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;GP = arcgisscripting.create(9.3)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;GP.overwriteoutput = 1&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;GP.workspace = "C:\\USER\\FLATHER\\great_plains_corridor\\experimental"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;rows = GP.SearchCursor("C:\\USER\\FLATHER\\great_plains_corridor\\experimental\\polys_dd.shp")&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;row = rows.reset()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;row = rows.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;for row in rows: # just in case, the following two lines are indented in the code but didn't appear indented in the preview&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; print row(0)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; row = rows.Next()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The error is:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Traceback (most recent call last):&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; exec codeObject in __main__.__dict__&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; File "C:\USER\FLATHER\great_plains_corridor\experimental\script3.py", line 23, in &amp;lt;module&amp;gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt; for row in rows:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;TypeError: 'geoprocessing cursor object' object is not iterable&lt;/SPAN&gt;&lt;BR /&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Apr 2012 14:12:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221277#M17105</guid>
      <dc:creator>MikeKnowles</dc:creator>
      <dc:date>2012-04-05T14:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221278#M17106</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A cursor "returns an enumeration object that will, in turn, hand out row objects". I'm not quite sure why (I started with Python in ArcGIS 10), but in 9.3 I think it likes "while row:" rather than "for row in rows:".&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Apr 2012 14:32:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221278#M17106</guid>
      <dc:creator>DarrenWiens2</dc:creator>
      <dc:date>2012-04-05T14:32:51Z</dc:date>
    </item>
    <item>
      <title>Re: iterating through a shapefile</title>
      <link>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221279#M17107</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;In addition to the &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;while row:&lt;/SPAN&gt;&lt;SPAN&gt; change, I needed to specify the fields I wanted to print.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;while row:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row.POLYS_DD_, row.polys_dd_i&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row = rows.Next()&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 05 Apr 2012 15:38:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/iterating-through-a-shapefile/m-p/221279#M17107</guid>
      <dc:creator>MikeKnowles</dc:creator>
      <dc:date>2012-04-05T15:38:17Z</dc:date>
    </item>
  </channel>
</rss>

