This should be a simple answer ... I'm trying to just remove the first 25 characters from a list (>90 in number) of Feature classes (raster datasets) within a .gdb.
All the file names are named the same except for the last 9 digits (hence why I am trying to remove the first 25).
I would then like to add some standard text to all the FCs (in the example "Hello")
My attempt from other threads comprises the following (but has not worked so far):
-------------------
import arcpy
import os
dir = r'C:\Users\X\Desktop\X\X.gdb'
arcpy.env.workspace = dir
for ras in arcpy.ListRasters ("*"):
print (ras) #original raster name
basename = ras.split("*")[:25] #strips the first 25 characters from original raster name
newrastername = "Hello" + basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name
----------------------
Error received:
--------------------------------------------------------------------------- SyntaxError Traceback (most recent call last) File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py, in parse: Line 35: return compile(source, filename, mode, PyCF_ONLY_AST) SyntaxError: Missing parentheses in call to 'print'. Did you mean print(ras #original raster name)? (<string>, line 😎
Solved! Go to Solution.
import arcpy
import os
dir = r'C:\Users\X\Desktop\X\X.gdb'
arcpy.env.workspace = dir
for ras in arcpy.ListRasters ("*"):
#print (ras) #original raster name
#print(ras.split("*"))
basename = ras[25:] #strips the first 25 characters from original raster name
newrastername = "Hello" + basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name
Can you share your print statement returns and your error messages? It looks ok, but what is ras.split("*") for?
yes apologies
It is to try to remove the first 25 (took the split from some research on line, but likely not the most sensible function to use)
well that's not quite right. Just use
basename = ras[:25]
and forget about the .split("*") unless you want to create a list of strings split at an asterisk *. in that case use
basename = ras.split("*")[0][:25]
Edit - I also think you need [25:] not [:25]
split produces pieces, you have to determine which one first then slice off the characters. For example
ras = "left*right"
ras.split("*")[0][:2]
'le'
ras.split("*")[1][:2]
'ri'
ras.split("*")[0][-2:]
'ft'
ras.split("*")[1][-2:]
'ht'
or skip the split
ras[-5:]
'right'
ras[:5]
'left*'
thank you. However I'm still getting an 'indentation' error:
IndentationError Traceback (most recent call last) File C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\Lib\ast.py, in parse: Line 35: return compile(source, filename, mode, PyCF_ONLY_AST) IndentationError: expected an indented block (<string>, line 10)
Original code:
import arcpy
import os
dir = r'C:\Users\Michael.Tomiak\Desktop\25\Copy.gdb'
arcpy.env.workspace = dir
for ras in arcpy.ListRasters ("*"):
print (ras)
basename = ras[25:] #strips the first 25 characters from original raster name
newrastername = basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name
import arcpy
import os
dir = r'C:\Users\X\Desktop\X\X.gdb'
arcpy.env.workspace = dir
for ras in arcpy.ListRasters ("*"):
#print (ras) #original raster name
#print(ras.split("*"))
basename = ras[25:] #strips the first 25 characters from original raster name
newrastername = "Hello" + basename
print (newrastername)
arcpy.CopyRaster_management(ras, newrastername) #create a new raster with the new/updated name
Thanks. Worked well. Didn't know indentation was required for it to run. Thought it was a presentation aspect.
WRT to the last line of code, what function could replace this so that it was only renaming the original files, rather than making a copy of the data (which is useful sometimes) but I do not have much data storage capacity left. Making a copy might be tight.
Would it be something like:
arcpy.RenameRaster_management (ras, newrastername)
import arcpy
#import os #youre not using os so dont import it
dir = r'C:\Users\X\Desktop\X\X.gdb'
arcpy.env.workspace = dir
for ras in arcpy.ListRasters ("*"):
#print (ras) #original raster name
#print(ras.split("*"))
basename = ras[25:] #strips the first 25 characters from original raster name
newrastername = "Hello" + basename
print (newrastername)
arcpy.Rename_management(ras, newrastername) #rename raster with the new/updated name
Worked a treat 😀- thanks for your time and effort David. Saved me an awful lot of manual renaming tonight.