Rename loaded raster in ArcMap through ArcPy

01-28-2023 06:11 AM
Occasional Contributor

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
1 Reply
Occasional Contributor III

You do not need to remove data from an mxd to unlock the data, you can simply close the mxd. You can then run the python script in an IDE like IDLE/PyCharm/PyScripter to rename, or if youre more comfortable running the script in ArcMap just open a new mxd without any data added and run. 

~ Mapping my way to retirement
0 Kudos