Select to view content in your preferred language

how to execute an python arcsript

1805
8
Jump to solution
07-11-2012 04:16 PM
ElaineKuo
Regular Contributor
System: ArcGIS 9.3

Problem

I want to use a code to split rows using a script downloaded from arcscript. (attached)
However, an error message showed that

"AttributeError: Object: Tool or environment <SplitByAttribute> not found"

Please kindly advise modification on the following code and thank you.

##Script Name: split rows ##Description: using  ##Created By: Elaine Kuo ##Date: 12/07/2012   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)   #Set the workspace. gp.Workspace= "H:/temp/test"   #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"  # Add a toolbox with a model to the geoprocessor and set the workspace gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/Split rows.tbx")   #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()  # Loop through every item in the list that was just generated for fc in fcs:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name            #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      #get file name     fcName, fcExt = os.path.splitext(fc)       #build field name     fieldName = fcName      #### Execute Script Tool ####     gp.SplitByAttribute(fc, fieldName, outWorkspace, fieldName)     gp.AddMessage(gp.GetMessages()) print gp.GetMessages()                            
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Frequent Contributor
The error sugests that the toolbox you added
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/Split rows.tbx")
does not contain SplitByAttribute tool.

You can access this toolbox and tool in any location on your computer like:
gp.AddToolbox(r"C:\Temp\AdditionalAnalysis\Additional Analysis - Generic Tools.tbx")
The Script folder has to be in the same parent folder as .tbx toolbox.

Moreover, you can add toolbox alias (which is analysis2 in this case) to tool name:
gp.SplitByAttribute_analysis2(...)

View solution in original post

0 Kudos
8 Replies
MarcinGasior
Frequent Contributor
The error sugests that the toolbox you added
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/Split rows.tbx")
does not contain SplitByAttribute tool.

You can access this toolbox and tool in any location on your computer like:
gp.AddToolbox(r"C:\Temp\AdditionalAnalysis\Additional Analysis - Generic Tools.tbx")
The Script folder has to be in the same parent folder as .tbx toolbox.

Moreover, you can add toolbox alias (which is analysis2 in this case) to tool name:
gp.SplitByAttribute_analysis2(...)
0 Kudos
ElaineKuo
Regular Contributor
Thanks for the response.

However, the location of the python tool is wrong and the code based on your modification did not work.
(The tool works well using arcGIS Desktop).

Please kindly advise how to make the source location of the python code correct.
Thank you.
(not File "H:\python_p\python_code\split_row_0717.py", should be File "C:\Program Files\ArcGIS\ArcToolbox\Scripts\SplitFC.py")
(I changed the source location using ArcGIS Desktop and ArcCatalog but did not work.)


The error message is as followed:

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "H:\python_p\python_code\split_row_0717.py", line 46, in <module>
    gp.SplitByAttribute_Analysis2(fc, fieldName, outWorkspace, fieldName)
AttributeError: Object: Tool or environment <SplitByAttribute_Analysis2> not found
0 Kudos
MarcinGasior
Frequent Contributor
Unzip the AdditionaAnalysis.zip to some folder.
Don't touch any files in unzipped folder.
Then just reference whole toolbox with tool of interest.

For location like in this image:
[ATTACH=CONFIG]16155[/ATTACH]
the reference path will be:
gp.AddToolbox(r"C:\Temp\AdditionalAnalysis\Additional Analysis - Generic Tools.tbx")
0 Kudos
ElaineKuo
Regular Contributor
Thank you for the response.

I modified the code and copied the unzipped folder to the ArcToolbox folder.

# Add a toolbox with a model to the geoprocessor and set the workspace
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolBox/Toolboxes/AdditionalAnalysis/Additional Analysis - Generic Tools.tbx")


However, the same error still existed.
0 Kudos
MarcinGasior
Frequent Contributor
That's strange.
When you browse in ArcCatalog to this toolbox, do you see something like this:
[ATTACH=CONFIG]16182[/ATTACH]

Can you run SplitByAttribute tool (which alias is Split Feature Class By Attribute) from ArcCatalog?
0 Kudos
ElaineKuo
Regular Contributor
Yes, the tool exists as the same of your picture.
Also, it works in ArcCatalog.

Thank you again.
0 Kudos
ElaineKuo
Regular Contributor
System: 9.3
Windows: vista

Problem: I am using the following code to execute a tool to split row in the shapefile in attached test1.
It ran smoothly without errors but no outcome appeared.
The tool is also attached as AdditionAnalysis.

Please kindly advise any improvement and thank you in advance.

Elaine


##Script Name: split rows
##Description: using 
##Created By: Elaine Kuo
##Date: 23/09/2012


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)


#Set the workspace.
gp.Workspace= "H:\temp\test1"


#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:\temp"

# Add a toolbox with a model to the geoprocessor and set the workspace
gp.AddToolbox("C:\TEMP\AdditionalAnalysis\Additional Analysis - Generic Tools.tbx")


#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

    # Break out the name, no path or extension, using the describe object.
    desc = gp.describe(fc)
    featureName = desc.name
      
    #Validate the new feature class name for the output workspace.
    OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)

    #get file name
    fcName, fcExt = os.path.splitext(fc) 

    #build field name
    fieldName = fcName

    #### Execute Script Tool ####
    gp.SplitByAttribute_Analysis2(fc, fieldName, outWorkspace, fieldName)
  

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

        
   

            
0 Kudos
ElaineKuo
Regular Contributor
Finally, I made my own code.

##Script Name: split rows
##Description: to split featureclasses based on attributes
##Created By: Elaine Kuo
##Date: 24/09/2012


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)


#Set the workspace.
gp.Workspace= "H:/temp/test"

#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp/test"

#-----------------------------------------------------------------
#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

    # Break out the name, no path or extension, using the describe object.
    desc = gp.describe(fc)
    featureName = desc.name
    
    #   Get a list of the fields in the featureclass
    fields = gp.listFields(fc, "C*", "String")

#Select S 
#-----------------------------------------------------------------    
    
    # Loop through every item in the list that was just generated 
    for field in fields:

        gp.toolbox = "Data Management"
        gp.addMessage(type(field))

        # Select records (C*, i.e. C7658)
        # Make temporary featureclasses
        gp.toolbox = "Data Management"
        query = "\"%s\" = 'S'" % field.Name
    
        gp.select_analysis(fc,("outputS.shp"),query)

        #get file name
        fcName, fcExt = os.path.splitext(fc) 

        # replace the strings you want to
        fcName = fcName.replace("_Dissolve","S")
        fcName = fcName + fcExt

        gp.rename_management("outputS.shp", fcName)
        gp.delete(fc) 

#-----------------------------------------------------------------
#-----------------------------------------------------------------
        #get file name
        #fcName, fcExt = os.path.splitext(fc) 

        # replace the strings you want to
        #fcName = fcName.replace("_Dissolve"," ")
        #fcName = fcName + fcExt
        #gp.rename_management(fc, fcName)
      

        # clear memory of temporary shapefiles
        #gp.delete("outputS.shp")  
        

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

        
   

            
0 Kudos