Select to view content in your preferred language

Rename loaded raster in ArcMap through ArcPy

453
1
01-28-2023 06:11 AM
Manu
by
Regular 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.

E.g.

arcpy.Describe("path/to/file0")

does not work whereas with

arcpy.Describe("path/to/file1.bsq")

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

arcpy.management.Rename(file, file + extension)
# or
arcpy.Rename_management(file, filename + extension)

I get some

Runtime error: [...] File "c:\program files(x86)\arcgis\desktop10.8\arcpy\management.py", 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
Clubdebambos
Frequent Contributor

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. 

~ learn.finaldraftmapping.com
0 Kudos