Using CopyFeatures for python script

733
3
03-22-2011 06:42 AM
SimoneEhrenberger
New Contributor
Hi, I tried to write a small scritp which copies each row of a feature class to a new file. I wanted to use the CopyFeatures function to create the new files, but when I run the modul I get this error message back:

AttributeError: 'NoneType' object has no attribute 'CopyFeatures_management'

I am not sure what the problem exactly is and how to solve it. Any suggestions?

This is the code I wrote:
# Import arcpy module
import arcpy
from arcpy import env
import os

env.workspace = r"C:\ArcGIS\Mexico.gdb"

# Load required toolboxes
arcpy.ImportToolbox("C:\Programme\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Data Management Tools.tbx")

# Local variables:
cities_mexico = "cities_mexico"

searchCursor = arcpy.SearchCursor(cities_mexico)
row = searchCursor.next()

while row <> None:
    # Process: Copy Features        
    outFeature = os.path.join(env.workspace, row.CITY_NAME)
    print outFeature
    arcpy.CopyFeatures_management(row, outFeature)
Tags (2)
0 Kudos
3 Replies
KimOllivier
Occasional Contributor III
You would have to trap for a null record when there is a null value for cityname.



while row : 
    # Process: Copy Features 
    try:       
        outFeature = os.path.join(env.workspace, row.CITY_NAME)
        print outFeature
        arcpy.CopyFeatures_management(row, outFeature)
    except arcpy.ExecuteError:
        print "failed to write"
    row = searchCursor.next()

[You do not have to add toolboxes for system tools. This comes from generating a script from Modelbuilder where this redundant and non-portable line is added.]

Geoprocessing tools are designed to work on a whole featureclass or layer, not inside a cursor.
Your script will only write one feature to a new featureclass, overwriting any previous featureclass. Is this what you intend?

If you want more than one feature written out you would have to rewrite the process to select all the rows that meet an expression as a layer and then copy the layer to a new featureclass.

You would need a list of citynames from either running a cursor to collect them or maybe Frequency and then open a cursor to get the list. There is no need to set the output path if you have set the workspace.

arcpy.env.overwriteOutput = True
arcpy.Frequency_management(cities_mexico,"in_memory/citList","CITY_NAME")

for row in arcpy.SearchCursor("in_memory/citList"):
  arcpy.MakeFeatureLayer(cities_mexico,"temp_lay","CITY_NAME = '"+row.CITY_NAME+"'")
  arcpy.CopyFeatures("temp_lay",city)

(I have not run this in a Python window so there may be syntax and case errors in this)
0 Kudos
SimoneEhrenberger
New Contributor
Thank you for your remarks. I think, using a list could indeed solve the problem. The CopyFeature function obviously doesn't work with the searchCursor object.
But finally I just used the Select function and created a loop for the SQL expression. This script works fine now and copies each row of my feature class to a new seperate file:

# Set local variables
in_features = "cities_mexico"

searchCursor = arcpy.SearchCursor(in_features)
row = searchCursor.next()

for i in range(36):
    cityName = row.CITY_NAME

    # create output path name
    out_feature_class = os.path.join(env.workspace, cityName)
    
    where_clause = '"OBJECTID"='+str(i+1)

    # Execute Select
    arcpy.Select_analysis(in_features, out_feature_class, where_clause)
    row = searchCursor.next() 
0 Kudos
SimoneEhrenberger
New Contributor
Thank you for your remarks. I think, using a list could solve the problem. The CopyFeature function obviously doesn't work with searchCursor objects.

But finally I simply used the Select function and created a loop for the SQL expression. This script works fine now and copies each row of my input feature class to a separate file:

# Set local variables
in_features = "cities_mexico"

searchCursor = arcpy.SearchCursor(in_features)
row = searchCursor.next()

for i in range(36):
    cityName = row.CITY_NAME

    # create output path name
    out_feature_class = os.path.join(env.workspace, cityName)
    
    where_clause = '"OBJECTID"='+str(i+1)

    # Execute Select
    arcpy.Select_analysis(in_features, out_feature_class, where_clause)
    row = searchCursor.next()
0 Kudos