1

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?

1
  • I've edited the question and the code according to PolyGeo's answer, and I get a new error, described above.
    – Sn0W
    Feb 6, 2018 at 23:32

2 Answers 2

2

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 for arcpy.env.workspace.

I would use the Rename tool instead:

Changes the name of a dataset. This includes a wide variety of data types, among them feature dataset, raster, table, and shapefile.

Take care to distinguish the raster's layer name from its data source.

0

It is never good practice to slice a string (e.g. raster[4:13]) when renaming data because all that would be required for an error is one extra character in the string. instead, try to split the name up into parts by a common character if possible. In your case, you can split the string into parts by the underscore _ character. For example:

>>> name = "euj_LANDSAT7_250m_20170101_v2.tif"
>>> parts = name.split("_")
>>> parts
['euj', 'LANDSAT7', '250m', '20170101', 'v2.tif']
>>> final_name = parts[1] + "_" + parts[3] + ".tif"
>>> final_name
'LANDSAT7_20170101.tif'

I also see you have an undefined variable fileExtension which could be causing the error.

Applying the example to your case:

import arcpy
from arcpy import env

env.workspace = "E:/Training/TryArcpy/test01_2017"

in_data = arcpy.ListRasters()

for raster in in_data:
    parts = raster.split("_")
    out_data = parts[1] + "_" + parts[3] + ".tif"

    arcpy.Rename_management(in_data, out_data)
3
  • This looks good, but I still get the same error (the entire error message seen in my question). I looked at other threads with similar errors but didn't find anything useful that would help with this case. Any suggestions?
    – Sn0W
    Feb 7, 2018 at 17:32
  • Did you fix the variable issue?
    – Aaron
    Feb 7, 2018 at 17:44
  • I used ".tif" instead of the fileExtension.
    – Sn0W
    Feb 7, 2018 at 17:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.