<?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: Python Script: loop through selected features and and copy to geodatabase in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81837#M6526</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am assuming the "for loop" is not correctly structured?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 14 Apr 2014 06:44:58 GMT</pubDate>
    <dc:creator>DavidBailey</dc:creator>
    <dc:date>2014-04-14T06:44:58Z</dc:date>
    <item>
      <title>Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81834#M6523</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am working with surface types that I derived from LIDAR data. These surface types are in one feature class (polygon) - &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;associated with the feature class is a column called "grid_code"; the grid_code column has values from 0 to 145. These are elevation values and will be selected out to classify three surface types (ground surface, shrubs and trees). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to write a script that selects each surface type and creates a feature class for each of them. My output would have three&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;feature classes: ground, shrub and tree.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is the code I am working with but I am getting errors:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;import arcpy, os&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.workspace = aWS&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.env.overwriteOutput = True&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;try:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create query statements for each land cover type&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_tree = '"grid_code" &amp;gt; \'3\''&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_ground = '"grid_code" &amp;lt;= \'1\''&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in aListLIDAR:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", P303_layerL)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", aListLIDAR)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management(P303_layerL, "P303_" + str(item))&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del aListLIDAR&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del item&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; del P303_layerL&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;except:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Code to run when an error occured&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "An ERROR Occured!"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + arcpy.GetMessages()&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;else:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; # Message when there was no error&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Error occurred"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;# Script end message&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;print "\nEnd of the script!"&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 13 Apr 2014 19:35:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81834#M6523</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-13T19:35:18Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81835#M6524</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;what are the errors?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;enclose your script in code blocks so that they can be read&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Apr 2014 05:50:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81835#M6524</guid>
      <dc:creator>DanPatterson_Retired</dc:creator>
      <dc:date>2014-04-14T05:50:13Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81836#M6525</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I put the script in a code block - It is not telling me the error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I am new to python, and I just threw this script together by reading other python scripts. Let me know if you need more info&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Developed by: David Bailey
# Description: Providence Creek Watershed: land cover processing


# import arcpy site-package and os module
import arcpy, os

# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"
arcpy.env.workspace = aWS

# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#&amp;nbsp;&amp;nbsp;&amp;nbsp; automatically overwrite any existing output 
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create query statements for each land cover type
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_tree = '"grid_code" &amp;gt; \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_ground = '"grid_code" &amp;lt;= \'1\''

&amp;nbsp;&amp;nbsp;&amp;nbsp; aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in aListLIDAR:
&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; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", P303_layerL)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", aListLIDAR)
&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; arcpy.CopyFeatures_management(P303_layerL, "P303_" + str(item))

&amp;nbsp;&amp;nbsp;&amp;nbsp; del aListLIDAR
&amp;nbsp;&amp;nbsp;&amp;nbsp; del item
&amp;nbsp;&amp;nbsp;&amp;nbsp; del P303_layerL

&amp;nbsp;&amp;nbsp;&amp;nbsp; 
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Code to run when an error occured
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "An ERROR Occured!"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + arcpy.GetMessages()

else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Message when there was no error
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Error occurred"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Script end message
print "\nEnd of the script!"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:07:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81836#M6525</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2021-12-10T23:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81837#M6526</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I am assuming the "for loop" is not correctly structured?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Apr 2014 06:44:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81837#M6526</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-14T06:44:58Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81838#M6527</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I put the script in a code block - It is not telling me the error. &lt;BR /&gt;&lt;BR /&gt;I am new to python, and I just threw this script together by reading other python scripts. Let me know if you need more info&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Developed by: David Bailey
# Description: Providence Creek Watershed: land cover processing


# import arcpy site-package and os module
import arcpy, os

# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"
arcpy.env.workspace = aWS

# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#&amp;nbsp;&amp;nbsp;&amp;nbsp; automatically overwrite any existing output 
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create query statements for each land cover type
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_tree = '"grid_code" &amp;gt; \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_ground = '"grid_code" &amp;lt;= \'1\''

&amp;nbsp;&amp;nbsp;&amp;nbsp; aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
&amp;nbsp;&amp;nbsp;&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; for item in aListLIDAR:
&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; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", P303_layerL)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", aListLIDAR)
&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; arcpy.CopyFeatures_management(P303_layerL, "P303_" + str(item))

&amp;nbsp;&amp;nbsp;&amp;nbsp; del aListLIDAR
&amp;nbsp;&amp;nbsp;&amp;nbsp; del item
&amp;nbsp;&amp;nbsp;&amp;nbsp; del P303_layerL

&amp;nbsp;&amp;nbsp;&amp;nbsp; 
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Code to run when an error occured
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "An ERROR Occured!"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + arcpy.GetMessages()

else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Message when there was no error
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Error occurred"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Script end message
print "\nEnd of the script!"&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;A few questions and observations.&amp;nbsp; First is "P303_LIDAR_Poly" a feature class that is directly within your geodatabase, or is it in a feature class within the feature dataset?&amp;nbsp; If its in a feature dataset within the geodatabase, your environment for that part needs to be within that feature dataset it resides.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Second, "arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", aListLIDAR)" is wrong.&amp;nbsp; it should be "arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", item)".&amp;nbsp; Right now it is using the whole list for your SQL expression, which is a list.&amp;nbsp; You want it to run through each expression individually, so use item.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:08:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81838#M6527</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2021-12-10T23:08:02Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81839#M6528</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the response.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"P303_LIDAR_Poly" is a feature class directly in my geodatabase - I did not create a feature dataset.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I changed the "arcpy.SelectLayerByAttribute_management" tool to (arcpy.SelectLayerByAttribute_management(P303_layerL, "NEW_SELECTION", item)" but I still had an error. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I have my code written where it will tell me what the error was - but for some reason it is not giving me the messages.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 14 Apr 2014 16:24:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81839#M6528</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-14T16:24:57Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81840#M6529</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Well there seems to be a couple issues here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;In your arcpy.MakeFeatureLayer_management method, your outlayer is being written to a variable "P303_layerL" that was never declared anywhere, that would certainly throw an error.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I made some changes to your script and posted them below here.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Some of the changes I made in your for loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;[INDENT]I integrated the select by attributes method right into the Make Feature Layer, since it has an optional where clause where you put your query.&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//00170000006p000000" rel="nofollow noopener noreferrer" target="_blank"&gt;Make Feature Layer Documentation&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I changed your naming in the copy feature layer to reflect which layer you're copying by vegetation type.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I added in a delete step at the end to remove the temporary feature layer before you create another one on the next loop.&amp;nbsp; Not deleting the feature layer before the next step may cause issues.[/INDENT]&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hopefully this code works for you!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm happy to help you tweak it further if you're still having some issues.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Developed by: David Bailey
# Description: Providence Creek Watershed: land cover processing


# import arcpy site-package and os module
import arcpy, os

# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"
arcpy.env.workspace = aWS

# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#&amp;nbsp;&amp;nbsp;&amp;nbsp; automatically overwrite any existing output 
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create query statements for each land cover type
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_tree = '"grid_code" &amp;gt; \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_ground = '"grid_code" &amp;lt;= \'1\''

&amp;nbsp;&amp;nbsp;&amp;nbsp; aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layerName = ["tree","shrub","ground"]

&amp;nbsp;&amp;nbsp;&amp;nbsp; n = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for query in aListLIDAR:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer", where_clause=query)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("temp_layer", "P303_{0}_layer".format(layerName&lt;N&gt;))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("temp_layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1

except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Code to run when an error occured
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "An ERROR Occured!"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + arcpy.GetMessages()
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Script end message
print "\nEnd of the script!"&lt;/N&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:08:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81840#M6529</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-10T23:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81841#M6530</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;The arcpy.Delete_management("temp_layer") should be outside of the for loop, yet on the same indentation. You want the temp layer to be deleted after you have processed the items in the list.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Apr 2014 12:13:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81841#M6530</guid>
      <dc:creator>benberman</dc:creator>
      <dc:date>2014-04-15T12:13:26Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81842#M6531</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks for the responses. The new script looks good and it ran without any errors - however when I opened my three new land&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;surface types (P303_ground_layer, P303_shrub_layer, and P303_tree_layer) they had the same attributes as "P303_LIDAR_poly".&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It seems as if it did not process the query statements.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2014 06:21:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81842#M6531</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-16T06:21:34Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81843#M6532</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer", where_clause=query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;should probably be &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer", query)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;As I recall, you don't have to put the word where_clause in the parameters, its understood since its the 3rd parameter for make feature layer.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also Matt&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;&lt;BR /&gt;&lt;BR /&gt; In your arcpy.MakeFeatureLayer_management method, your outlayer is being written to a variable "P303_layerL" that was never declared anywhere, that would certainly throw an error.&lt;BR /&gt;&lt;BR /&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;its not being written to a variable, its a string, and since its an output parameter, it doesn't need to exist already, you are just naming your output layer name, it no different then what you changed it to.&amp;nbsp; "temp_layer" has no special meaning, its just a name you are giving it to store in memory.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 16 Apr 2014 13:09:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81843#M6532</guid>
      <dc:creator>IanMurray</dc:creator>
      <dc:date>2014-04-16T13:09:49Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81844#M6533</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;This is what my code looks like now (It works - but my outputs still have the same attributes as the P303_LIDAR_Poly):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;# Developed by: David Bailey
# Description: Providence Creek Watershed: land cover processing


# import arcpy site-package and os module
import arcpy, os

# Set geoprocessing environment:
# a) set the workspace environment 
aWS = r"C:\Thesis_Research\GIS\Prov_classification.gdb"
arcpy.env.workspace = aWS

# Set geoprocessing environment:
# b) the overwriteOutput parameter controls whether tools will
#&amp;nbsp;&amp;nbsp;&amp;nbsp; automatically overwrite any existing output 
arcpy.env.overwriteOutput = True

try:
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Create query statements for each land cover type
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_tree = '"grid_code" &amp;gt; \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_shrub = '"grid_code" = \'2\' OR "grid_code" = \'3\''
&amp;nbsp;&amp;nbsp;&amp;nbsp; LIDAR_ground = '"grid_code" &amp;lt;= \'1\''

&amp;nbsp;&amp;nbsp;&amp;nbsp; aListLIDAR = [LIDAR_tree, LIDAR_shrub, LIDAR_ground]
&amp;nbsp;&amp;nbsp;&amp;nbsp; layerName = ["tree","shrub","ground"]

&amp;nbsp;&amp;nbsp;&amp;nbsp; n = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for query in aListLIDAR:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer", query)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("temp_layer", "P303_{0}_layer".format(layerName&lt;N&gt;))
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("temp_layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1

&amp;nbsp;&amp;nbsp;&amp;nbsp; 
except:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Code to run when an error occured
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "An ERROR Occured!"
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\n" + arcpy.GetMessages()

else:
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Message when there was no error
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "\nNo Error occurred"
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
# Script end message
print "\nEnd of the script!" &lt;/N&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:08:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81844#M6533</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2021-12-10T23:08:07Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81845#M6534</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hmmmm,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It sounds like the query isn't working when the feature layer is being made.&amp;nbsp; To figure out what's going on, my suggestion would be to make your feature layer and then run a select by attribute.&amp;nbsp; I put a code snippet below.&amp;nbsp; Try replace your for loop with that code.&amp;nbsp; I also added in an arcpy.AddMessage that should echo out the Query you're using for each layer.&amp;nbsp; This isn't really required, just included that for error checking.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; n = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; for query in aListLIDAR:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("P303_LIDAR_Poly", "temp_layer")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # You can use "print" instead of "arcpy.AddMessage" if you're directly in the console.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Query For: {0}: {1}".format(layerName&lt;N&gt;,query))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management ("temp_layer", "NEW_SELECTION",query)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CopyFeatures_management("temp_layer", "P303_{0}_layer".format(layerName&lt;N&gt;))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management("temp_layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1&lt;/N&gt;&lt;/N&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Are there three layers being made with the names&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;P303_tree_layer,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;P303_shrub_layer,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;P303_ground_layer &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;currently?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 23:08:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81845#M6534</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-10T23:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81846#M6535</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Yes, with my current script I am getting three different layers with the names: P303_tree_layer, P303_shrub_layer, P303_ground_layer - however they are all the same. They should be different according to the query that was created.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I tried the code you gave me and it came back with this error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Executing: SelectLayerByAttribute temp_layer NEW_SELECTION ""grid_code" &amp;gt; '3'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Start Time: Sat Apr 19 15:13:12 2014&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used. [P303_LIDAR_Poly]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed at Sat Apr 19 15:13:12 2014 (Elapsed Time: 0.00 seconds)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;End of the script!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It appears that my SQL statements are not written correctly? I don't see where&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 19 Apr 2014 21:27:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81846#M6535</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-19T21:27:53Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81847#M6536</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Make sure to null out the quotes inside your expression using backslashes like this on all your SQL queries:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;"\"grid_code\" &amp;gt; '3'"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If you don't do that, Python will see those other quotes as symbols to close and open your string instead of as literal parts of your string.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 20 Apr 2014 21:45:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81847#M6536</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2014-04-20T21:45:09Z</dc:date>
    </item>
    <item>
      <title>Re: Python Script: loop through selected features and and copy to geodatabase</title>
      <link>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81848#M6537</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I did this and it still gives me this error:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;An ERROR Occured!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Executing: SelectLayerByAttribute temp_layer NEW_SELECTION ""grid_code" &amp;gt; '3'"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Start Time: Tue Apr 22 23:29:17 2014&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR 000358: Invalid expression&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;An invalid SQL statement was used. [P303_LIDAR_Poly]&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed to execute (SelectLayerByAttribute).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Failed at Tue Apr 22 23:29:17 2014 (Elapsed Time: 0.00 seconds)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;End of the script!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Apr 2014 05:29:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/python-script-loop-through-selected-features-and/m-p/81848#M6537</guid>
      <dc:creator>DavidBailey</dc:creator>
      <dc:date>2014-04-23T05:29:57Z</dc:date>
    </item>
  </channel>
</rss>

