I have multiple raster layers (TIFF) with names following this pattern:
"euj_LANDSAT7_250m_20170101_v2"
"euj_LANDSAT7_250m_20170102_v2">
"euj_LANDSAT7_250m_20170103_v2">
...
and I would like to rename them to follow this pattern:
LANDSAT7_170101.tif"
LANDSAT7_170102.tif"
LANDSAT7_170103.tif"
...
I've tried to use this code (edited according to PolyGeo's answer):
import arcpy
from arcpy import env
env.workspace = "E:/Training/TryArcpy/test01_2017"
in_data = arcpy.ListRasters()
for raster in in_data:
out_data = raster[4:13] + raster[20:26] + fileExtension
arcpy.Rename_management(in_data, out_data)
The message I get is:
Runtime error
Traceback (most recent call last):
File "<string>", line 3, in <module>
File "c:\program files (x86)\arcgis\desktop10.5\arcpy\arcpy\management.py",
line 4537, in Rename
raise e
RuntimeError: Object: Error in executing tool
Is this because I have a raster list as in_data? If yes, how do I loop through multiple rasters without using a list, or use a list in appropriate way? And if no, what could be the problem?
Best Answer
I would not recommend renaming using
os
in this because your TIFF files may have associated files like world files (**.tfw
) that will be missed.Also, the
os
module is unaware of the setting you have forarcpy.env.workspace
.I would use the Rename tool instead:
Take care to distinguish the raster's layer name from its data source.