I'm using arcpy and i need to save a raster file with the looping raster's name inside the output's name, in other words, if the looping raster is named "20170823" it must save like "Example_20170823". I need to find something similar to 'Example_%20170823%' that we have in arcgis.

import arcpy
import os
from arcpy import env
from arcpy.sa import *

rasters = arcpy.ListRasters("*", "TIF")
arcpy.env.workspace = 'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL'   

for tiff in rasters: #the loop

    arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "", "", "3")
    arcpy.MakeRasterLayer_management(tiff, "Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast2.tif", "", "", "4")

    arcpy.CopyRaster_management("Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast1.tif", "copy_rast1", "","", "", "", "", "", "", "", "", "")
    arcpy.CopyRaster_management("Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL\\temp_rast2.tif", "copy_rast2", "","", "", "", "", "", "", "", "", "")

    Property1Result = arcpy.GetRasterProperties_management ("copy_rast1", "MINIMUM")
    Property2Result = arcpy.GetRasterProperties_management ("copy_rast2", "MINIMUM")

    Property1 = Property1Result.getOutput
    Property2 = Property2Result.getOutput

    Correcao1 = Minus ("copy_rast1", "Property1")
    Correcao2 = Minus ("copy_rast2", "Property2")

    Rastdiv1 = Minus ("Correcao2", "Correcao1")
    Rastdiv2 = Plus  ("Correcao2", "Correcao1")

    NDVI_tiff = Divide (Rastdiv1, Rastdiv2,'Z:\\04 - Temp\\Charles\\NDVI_tempo\\NAL_PL\\teste\\NDVI_%tiff%.tif') # here the name to replace %tiff%

The NDVI_%tiff% must take the information of the tiff's name to add in the final output file's name .


What you need to use is the Describe function. This is module that can be used to find various metadata about the dataset, including its name.

Use the following to find the name:

tiff_name = arcpy.Describe(tiff).name

In addition, if you want to append this to an output file name, please look at the python string operations help.

To append the name, use the following code, and use the variable in your geoprocessing function.

output_name = "{}_{}".format("Example", tiff_name) 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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