Select to view content in your preferred language

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

3018
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
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor
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,


That's what this does in my solution I provided:

 arcpy.ListRasters("*_pa") 


Edit: if you must absolutely ensure that the last 3 characters are evaluated, then you could use the .endswith property.  Here's a modified version of my OP:

 for ras in arcpy.ListRasters():     if ras.endswith("_pa"):          basename = ras.split("_pa")[0]         newrastername = "pa_" + basename         arcpy.CopyRaster_management(ras, newrastername) 

View solution in original post

0 Kudos
15 Replies
AdamCox1
Deactivated User
Here's some info on the correct tool to use:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000056000000

Here's an example of how to reconstruct the name:
name = "original_pa"
newname = "PA_" + name[:-3]


Hope that solves it!
0 Kudos
JamesCrandall
MVP Frequent Contributor
This should get you pretty close to what you need I think.

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

0 Kudos
AndrewTuleya2
Emerging Contributor
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,
0 Kudos
JamesCrandall
MVP Frequent Contributor
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,


That's what this does in my solution I provided:

 arcpy.ListRasters("*_pa") 


Edit: if you must absolutely ensure that the last 3 characters are evaluated, then you could use the .endswith property.  Here's a modified version of my OP:

 for ras in arcpy.ListRasters():     if ras.endswith("_pa"):          basename = ras.split("_pa")[0]         newrastername = "pa_" + basename         arcpy.CopyRaster_management(ras, newrastername) 
0 Kudos
hansaneefernando
Emerging Contributor

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

0 Kudos
MichaelTomiak
Occasional Contributor

What would be the syntax if you just wanted to remove the first X amount of character from the current file name? 

0 Kudos
AndrewTuleya2
Emerging Contributor
Thanks! Ill give it a go right now.
0 Kudos
AdamCox1
Deactivated User
The answers above don't take case into account, which is the variation that you mentioned.  You should add a .lower() function to the string. This will evaluate the string as a lowercase version of itself.  For example:
if ras.lower().endswith("_pa"):
    #rename the raster

Also, I just tested and the split function is case sensitive, so you'll have to watch out for that.  What I suggested above works in this way:
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

So, regardless of whether or not the raster ends in "_pa", the last three characters will be removed.  This may not be desirable, so you may want to filter the rasters by using the if statement that jamesfreddyc suggested.  Just be careful because python is case sensitive.
0 Kudos
Zeke
by
Honored 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.


for ras in arcpy.ListRasters():     
    if ras.lower.endswith("_pa"):             
        basename = ras.split("_pa")[0]         
        newrastername = "pa_" + basename         
        os.rename(ras, newrastername)
0 Kudos