Rename loaded raster in ArcMap through ArcPy

01-28-2023 06:11 AM
New Contributor III

I run a Python ToolBox in ArcMap with hyperspectral raster files (currently only testing with .bsq files) as input. However, it appears some functions do not work in case the filenames lack an extension.



does not work whereas with


I get the expected results.


Thus, I want to rename the files in case they lack an extension. To do so, I try to find a .hdr file, extract the extension and add it to the filename.

However, renaming the files apparently does not work. When I try to use

os.rename(filepath, filepath + extension)

I get an error because the file is used/locked by ArcMap. If I thy to use ArcPy with, file + extension)
# or
arcpy.Rename_management(file, filename + extension)

I get some

Runtime error: [...] File "c:\program files(x86)\arcgis\desktop10.8\arcpy\", line 4524, in Rename
raise e ExecuteError: ERROR 999999

This error message does not help much. Is there some workaround for that issue?


Worst case: I would have to remove the raster from the .mxd somehow, rename it using os.rename and subsequently add it back to the map; however, I am not sure whether this is possible with the raster file path only (the Toolbox allows entering the full path), so perhaps all instances of the raster need to be removed if it was loaded multiple times... I hope there are better solutions?

0 Kudos
0 Replies