Error 999999 while renaming raster.

613
6
07-15-2013 12:39 PM
ScottBarron
New Contributor III
Hi,
I keep getting error 999999 while trying to rename a raster. The code is below. Any advice?

for raster in arcpy.ListRasters():
    #raster_name_check= raster[40]
    if len(raster)>=40:
        # get the raster name and file extension
        fileName,fileExtension = os.path.splitext(raster)

        #fileNameParts = fileName.split('_')
        compactFileName = fileName[7:12] + fileName[23:29]
        arcpy.CopyRaster_management(raster, compactFileName)
        compactFileName2= fileName[7:12] + fileName[23:29] + fileName[34:38]
        arcpy.Rename_management(compactFileName,compactFileName2)
        raster_list.append(compactFileName2)
        print raster_list
    if len(raster)<=40:
        print "Skip"
Tags (2)
0 Kudos
6 Replies
DanPatterson_Retired
MVP Emeritus
if the raster name is too long it wont work print out the name and ensure it is less that 13 characters
0 Kudos
ScottBarron
New Contributor III
I've tried that, still no good. I'm pretty sure I got it to work the very first time I tried it, but I have no idea whats different now.
0 Kudos
RichardFairhurst
MVP Honored Contributor
I've tried that, still no good. I'm pretty sure I got it to work the very first time I tried it, but I have no idea whats different now.


You can't rename anything twice in a row to the same name, only once.  That is what is different.  The first time no object had the name you were renaming to.  The second time an object already had that name and you tried to rename another object with the same name, resulting in a conflicting duplicate name.  After the first time you rename an object to something, you have to delete the object that has that name before you can rename another object with that name.
0 Kudos
ChrisSnyder
Regular Contributor III
ESRI GRID format has a limit of 13 characters, but it appears you are using a different format (something with a file extension like a .tif or .img or something).

Make sure you don't have the file you are attempting to rename open in another application.

What is the raster format you are using and the new name you are trying to rename to?
0 Kudos
RichardFairhurst
MVP Honored Contributor
The problem is he ran the script once and created a copy of an object assigning it a name.  Then he ran the script again on the same original objects and tried to create another copy of that object with the name he had previously assigned to the first copy.  Two objects cannot share the same file name and the rename function ignores the overwrite outputs setting.  With Rename the second time you run a script to reuse a name, you must add a delete routine.

Anytime I configure a Rename script I actually have to run it 2 times to create a script that will run correctly from that point on.  First using just a rename, the second time deleting the previously renamed object and then renaming another copy with that name (the delete function made no sense during the first run, but it is needed during the second).  The third time and following the script needs no modification, since it will remove the existing object with a given name and then rename a new object to that name.
0 Kudos
RhettZufelt
MVP Frequent Contributor
I would normally put something like this in there so it only needs run once:

          if arcpy.Exists(compactFileName2):
             arcpy.Delete_management(compactFileName2)


Also, keep in mind that if you are saving as a FGDB raster, you can't start the filename with a number or special character.

R_
0 Kudos