python challenged

784
8
05-14-2010 10:58 AM
CariRuffino
New Contributor
I have about 7000 feature classes in a file geodatabase that need name ( need to remove 7 characters ) changes but I am rather clueless when it comes to code and am at a loss as to which tool I would use and what arguments I would need to use. Is there a anyone that can help?

example of the needed changes

*current features      
adl390012_ll_Blk 
                                                                             
*what I need the feature names to read
adl390012
0 Kudos
8 Replies
JoelCalhoun
New Contributor III
If you need to remove exactly 7 characters from the end of every featue class you can try this.

import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

gp.workspace = "X:\\assr\\carto\\process_temp\\Demo_data.gdb"     # Path to your file geodatabase

# Get a list of feature classes in the workspace.
fcs = gp.ListFeatureClasses()

for fc in fcs:                           
    str_length = len(fc)              # Get the length of the feature class name string
    new_str_len = str_length - 7      # Get the length minus the last 7 characters
    new_fc = fc[0:new_str_len]        # Builds the new string to the new length
    gp.rename_management(fc, new_fc)  # Rename the feature class


I hope that helps,

Joel
0 Kudos
LukePinner
New Contributor
Or this

import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
gp.workspace = "C:\\path_to_your\\geodatabase.gdb"

# Get a list of feature classes in the workspace.
fcs = gp.ListFeatureClasses()

for fc in fcs:                           
    gp.rename_management(fc, fc[:-7]) 
0 Kudos
CariRuffino
New Contributor
For the first suggested code I recieve an error message that reads:PythonWin 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)] on win32.
Portions Copyright 1994-2006 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
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 "C:\Documents and Settings\ceruffino\Desktop\scripts\name.py", line 15, in <module>
    gp.rename_management(fc, new_fc)  # Rename the feature class
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Output data element: Value is required
Failed to execute (Rename).



For the second suggested code I recieve this error message
File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\ceruffino\Desktop\scripts\name.py", line 12, in <module>
    gp.rename_management(fc,fc[:-7])
ExecuteError: Failed to execute. Parameters are not valid.
ERROR 000735: Output data element: Value is required
Failed to execute (Rename).

I unfortunately am not yet skilled enough in Python to figure this out on my own . Any suggestions are welcome. ~Cari
0 Kudos
JoelCalhoun
New Contributor III
Can you post the exact code you used?  It seems that in both cases it is not reading an output value for the rename operation.


Joel
0 Kudos
CariRuffino
New Contributor
Here is the first code

import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

gp.workspace = "C:\\Documents and Settings\\ceruffino\\Desktop\\Closed State Leases\\Closed State Leases.gdb"     # Path to your file geodatabase

# Get a list of feature classes in the workspace.
fcs = gp.ListFeatureClasses()

for fc in fcs:                          
    str_length =len(fc)
    new_str_len=str_length -7
    new_fc =fc[0:new_str_len]
    gp.rename_management(fc,new_fc)
0 Kudos
RDHarles
Occasional Contributor
Your code works.  The only thing I changed to test it was the path to the FGDB.  I have to assume the path is incorrect in some way.


import arcgisscripting, os
gp = arcgisscripting.create(9.3)

gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")

gp.workspace = "C:\\junk\\temp.gdb" # Path to your file geodatabase

# Get a list of feature classes in the workspace.
fcs = gp.ListFeatureClasses()

for fc in fcs:                           
    str_length =len(fc)
    new_str_len=str_length -7
    new_fc =fc[0:new_str_len]
    gp.rename_management(fc,new_fc)                         
0 Kudos
JoelCalhoun
New Contributor III
Out of curiosity, are you using ArcGIS 9.3.1? or are you still using ArcGIS 9.2?

If you are still on ArcGIS 9.2 you cannot use the 9.3 geoprocessor object:
gp = arcgisscripting.create(9.3) would have to be gp = arcgisscripting.create() at 9.2.

ESRI also changed the way the gp.ListFeatureClasses works between those two releases.
At 9.2 it would return an enumeration object but at 9.3 it returns a Python List.
This changes the way you loop through the feature classes.

Also I would recommend against using spaces in filepaths.
I think ESRI has a validation tool but an easier solution would be moving your folder and gdb to C: and using underscores.

See example below:

gp.workspace = "C:\\Closed_State_Leases\\Closed_State _Leases.gdb"


I don't know if any of those will solve your problem but I hope you can get it figured out.

Joel
0 Kudos
CariRuffino
New Contributor
I am not sure what happened. I know that certain characters in the string were removed but it was not actually -7 it was -5. I tried putting the gdb directly on the C and replaced  spaces with underscores but that also proved to be unsuccessful. I currently am using ArcInfo 9.3.1 so that shouldn't be an issue. I'm sure it's some tiny detail that's stalling my progress but I can't seem to get around it.
0 Kudos