Select to view content in your preferred language

Removing characters from rasters and putting them to the front of file name

2970
15
Jump to solution
05-29-2014 05:46 AM
AndrewTuleya2
Emerging Contributor
I need syntax in Arcpy/python that will remove "_pa" from the end of any raster file that has this at the end of its file name. Additionally, on files where this is the case, I want to add "PA_" to the front of these files. Can anyone help me?

Thanks.
Tags (2)
0 Kudos
15 Replies
JamesCrandall
MVP Frequent Contributor
The answers above don't take case into account, which is the variation that you mentioned.


Good catch, although I didn't get that from the OP.  But yep case will need to be handled if it is important to the implementation.  One minor change could be made using:


if ras.endswith("_pa") or ras.endswith("_PA"):

0 Kudos
AdamCox1
Deactivated User
Yeah that would work.  Usually I use the following format in order to reduce indentation:

for ras in arcpy.ListRasters:
    if not ras.lower().endswith("_pa"):
        continue
    #now you are working with the correct raster, and you can do what you need
    arcpy.management.Rename(ras,"PA_" + ras[:-3])


Though it may be tricky to get all of the auxiliary files if you are using os.rename(), right?  I assume that it's not just "raster.tif", but also "raster.tif.xml" and "raster.tfw", or .jpg, etc.  If you use the arcpy function, it'll rename all of the associated files at once.  Also, if the rasters are stored in a fGDB or something, I don't there's anyway you'd be able to access them with the os module.  It all depends on how/where they are stored.
0 Kudos
JamesCrandall
MVP Frequent Contributor
If you're importing os anyway, you can also use os.rename() rather than copying the raster. Rest is what James & Adam wrote about getting the name correctly.


os.rename() failed on a GDB raster.  But I also tried 

arcpy.Rename_management(ras, newrastername)


but it failed with a "table already exists" error.
0 Kudos
Zeke
by
Honored Contributor
Though it may be tricky to get all of the auxiliary files if you are using os.rename(), right?  I assume that it's not just "raster.tif", but also "raster.tif.xml" and "raster.tfw", or .jpg, etc.  If you use the arcpy function, it'll rename all of the associated files at once.  Also, if the rasters are stored in a fGDB or something, I don't there's anyway you'd be able to access them with the os module.  It all depends on how/where they are stored.

I've never used it before, but the Rename function that I linked to above looks super useful.


You may be right. I've used os.rename on rasters, but using os.listdir(). Rename in your link does look promising.
0 Kudos
AdamCox1
Deactivated User
but it failed with a "table already exists" error.


Sounds like the newrastername already exists in the GDB?

I would do a little inspection of the contents of the GDB, but if you need to remove something before proceeding, this is a useful construction:

if arcpy.Exists(newrastername):
    arcpy.management.Delete(newrastername)


Just be sure to back everything up ahead of time!
0 Kudos
JamesCrandall
MVP Frequent Contributor
Sounds like the newrastername already exists in the GDB?


Something was going on in the .gdb I guess, it was weird.  After a quick restart the arcpy.Rename_management seems to work.  This def looks like a really useful method!
0 Kudos