Select to view content in your preferred language

rename_management failed

1406
6
Jump to solution
01-03-2013 01:04 PM
ElaineKuo
Regular Contributor
System ArcGIS 9.3
python 2.5

Hello,

I have 100 polygon shapefiles in a folder (file names: geoc0923, geoc7634, and geoc3923).
Each of them has a field called CXXXX (string).
For example, geoc0923 has the field C0923.

I tried to dissolve the 100 polygon shapefiels based on the CXXXX field ("R").
The dissolve process went smoothly, but some error appeared when renaming the output shapefile.

The error shows
gp.rename_management("outputR.shp", fcName)
ExecuteError: ERROR 000012: H:/temp/test\geoc0387.shp already exists
Failed to execute (Rename).

Please kindly advise correction on the code below.
thank you

#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"  #----------------------------------------------------------------- #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 R  #-----------------------------------------------------------------              # 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\" = 'R'" % field.Name              gp.select_analysis(fc,("outputR.shp"),query)          gp.Dissolve_management(fc,("outputR.shp"), field.Name)          #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("outputR.shp", fcName)         gp.delete("outputR.shp")      gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
T__WayneWhitley
Honored Contributor
Hi Elaine, I see you are starting the new year trying to wrap up a task from yesteryear --- well, hope you are close to the finish so you can open up the year with new and exciting challenges, what do you say, are you ready?

I'd like to start with the obvious- 1st of all, you do not have the overwriteoutput property set, so that's 1 reason your code throws the error.  Moreover, you have a nested loop - looping on fields and in turn looping on fcs...it doesn't really make sense to create an output based on input, then within the loop attempt overwriting the same fc you are using for input, do you see?

I am going to guess based on your variables toward the beginning that you'd like to fetch all fcs from an input directory (which i assume is working fine) and use an output directory as you have attempted to define with outWorkspace?  You have to 'assemble' the appropriate pathname, then you won't have the renaming error - everything will be going to the root you defined...there are several 'less than optimal' practices going on in your code, but let's start with isolating the direct cause of your error.

Have I made the correct assumption that outWorkspace is your intended output folder?

View solution in original post

0 Kudos
6 Replies
T__WayneWhitley
Honored Contributor
Hi Elaine, I see you are starting the new year trying to wrap up a task from yesteryear --- well, hope you are close to the finish so you can open up the year with new and exciting challenges, what do you say, are you ready?

I'd like to start with the obvious- 1st of all, you do not have the overwriteoutput property set, so that's 1 reason your code throws the error.  Moreover, you have a nested loop - looping on fields and in turn looping on fcs...it doesn't really make sense to create an output based on input, then within the loop attempt overwriting the same fc you are using for input, do you see?

I am going to guess based on your variables toward the beginning that you'd like to fetch all fcs from an input directory (which i assume is working fine) and use an output directory as you have attempted to define with outWorkspace?  You have to 'assemble' the appropriate pathname, then you won't have the renaming error - everything will be going to the root you defined...there are several 'less than optimal' practices going on in your code, but let's start with isolating the direct cause of your error.

Have I made the correct assumption that outWorkspace is your intended output folder?
0 Kudos
T__WayneWhitley
Honored Contributor
So try this, provided you want output to go to that defined by your outWorkspace variable---

Enter the following line just before your line gp.rename...

fcName = outWorkspace + os.sep + fcName
0 Kudos
ElaineKuo
Regular Contributor
Hello

I followed the advice and corrected the code.
However, the resulting shapefile is named as geoc0923_shp.shp.
Actually, I want it to be called as geoc0923_dissolve.shp

Please kindly advise how to revise the section of " replace the strings you want to" in the code below.
BTW, encouraging greetings are thanked but could be shortened next time.
Happy New Year

#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"

#-----------------------------------------------------------------
#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) 

  
    #   Get a list of the fields in the featureclass
    fields = gp.listFields(fc, "C*", "String")

    # replace the strings you want to
    fcName = fcName.replace("_Dissolve","")
    fcName = fcName + fcExt
    fcName = outWorkspace + os.sep + fcName
    
    gp.Dissolve_management(fc,OutFeatureClass, field.Name)
0 Kudos
T__WayneWhitley
Honored Contributor
Actually, I don't think 'rename' will work that way take out the gp.rename line and replace it with copy features below (keep the previously inserted line from last post).

gp.CopyFeatures_management("outputR.shp", fcName)

Here is the copy features webhelp:
http://webhelp.esri.com/arcgisdesktop/9.3/index.cfm?TopicName=Copy%20Features%20(Data%20Management)
0 Kudos
ElaineKuo
Regular Contributor
Hello

I changed the code already.
Please follow the second last response.
0 Kudos
T__WayneWhitley
Honored Contributor
Hello Elaine, I'll be as brief as I can:

I see what you are trying to do...nice try with writing the Dissolve output directly to your outWorkspace but your new string variable, OutFeatureClass, is formed incorrectly.

ValidateTableName is 'looking' at the whole fc name provided, geoc0923.shp, so auto-replaces invalid characters like '.' with '_' and evaluates the output location to apply the necessary shapefile ext - not 'smart enough' to realize it's the same file extension and just does the grunt work...  The funny thing about your code is that you've included 3 different ways to attempt getting at the file name (minus ext) - using decribe, validate, and splitext.  Also, incorrect usage of 'replace'.

Since we're just dealing with shapefiles, why not simply do it this way?? (just working with what you already have in your loop):
# Loop through every item in the list that was just generated
for fc in fcs:
    #get file name
    fcName, fcExt = os.path.splitext(fc) 

    #   Get a list of the fields in the featureclass
    fields = gp.listFields(fc, "C*", "String")

    #   Is 'fields' supposed to contain a single field?  If so, then you need this line:
    field = fields[0]

    # replace the strings you want to
    fcName = os.path.join(outWorkspace, fcName + "_Dissolve") + fcExt
    
    gp.Dissolve_management(fc, fcName, field.Name)
0 Kudos