Solved! Go to Solution.
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
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)
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?
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)