Thanks for the reply! What if the file name is not consistent? For example, one raster in the geodatabase is called York_pa and another is Harrisburg_PA, and I want the command to take care of all file names with _pa as the last 3 characters?Thanks,
arcpy.ListRasters("*_pa")
for ras in arcpy.ListRasters(): if ras.endswith("_pa"): basename = ras.split("_pa")[0] newrastername = "pa_" + basename arcpy.CopyRaster_management(ras, newrastername)
name = "original_pa" newname = "PA_" + name[:-3]
import arcpy import os dir = r'H:\Documents\ArcGIS\Default.gdb' arcpy.env.workspace = dir for ras in arcpy.ListRasters("*_pa"): print ras #original raster name basename = ras.split("_pa")[0] #strip _pa from original raster name newrastername = "PA_" + basename print newrastername arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name
if ras.lower().endswith("_pa"): #rename the raster
name1 = "harrisburg_pa" name2 = name1[:-3] #remove the last 3 characters, regardless of what they are name3 = "PA_" + name2 #adds "PA_" to the beginning of the new name
for ras in arcpy.ListRasters(): if ras.lower.endswith("_pa"): basename = ras.split("_pa")[0] newrastername = "pa_" + basename os.rename(ras, newrastername)
The answers above don't take case into account, which is the variation that you mentioned.
if ras.endswith("_pa") or ras.endswith("_PA"):
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])
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.
arcpy.Rename_management(ras, newrastername)
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.
but it failed with a "table already exists" error.
if arcpy.Exists(newrastername): arcpy.management.Delete(newrastername)
Sounds like the newrastername already exists in the GDB?
I used your code for the same application. the code runs fine and won't give me any issues. However I can't find the new rasters. Is there any way we can save the new rasters in a different folder?
My files are like this
20190512_160716_104b_3B_AnalyticMS_SR_clip
20190512_160717_104b_3B_AnalyticMS_SR_clip
This is the code I used
import arcpy import os dir = r'D:\My_Canola_Project\1 New Process\Testing 5 Images' arcpy.env.workspace = dir for ras in arcpy.ListRasters("*_clip"): print (ras) basename = ras.split("_clip")[0] newrastername = "I_" + basename print (newrastername ) arcpy.Rename_management(ras, newrastername)
Thanks in Advance
What would be the syntax if you just wanted to remove the first X amount of character from the current file name?
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.