I am processing multibands remote sensing rasters (i.e. Sentinel data). Each set of bands is stored in individual folder. I would like to list folders and list rasters (bands) in each folder to create a raster composite. Each new raster I would like to store under the folder name. How can I name the new raster by folder name?

# create raster composite per folder

import arcpy
import os

arcpy.env.workspace = "C:/Users/clipy/"  
outws = "C:/Users/output"

# list folders
folders = arcpy.ListWorkspaces()

arcpy.env.overwriteOutput = True 

for folder in folders:
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters("*.tif")
    name = os.path.join(outws, folder[1])
    arcpy.CompositeBands_management(rasters, name)

print "Raster composite complete"

I'm using ArcGIS 10.4 and python 2.7.

2 Answers 2


Building on Bjorn's answer, you need to specify the output raster name properly using something like the following:

raster_name = os.path.join(outws, folder.split("\\")[-1] + '.tif')

This strips the folder name from the path generated from ListWorkspaces(), which can then be used to create an output path and file name (including a file extension).

import arcpy, os

inws = r'C:\path\to\indata\directory'
outws = r'C:\path\to\outdata\directory'

arcpy.env.workspace = inws
folders = arcpy.ListWorkspaces()

for folder in folders:
    raster_name = os.path.join(outws, folder.split("\\")[-1] + '.tif')
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters()
    arcpy.CompositeBands_management(";".join(rasters), raster_name)

rasters contains a list of the raster files in the workspace. CompositeBands needs that list converted to a semicolon-delimited string. Change the CompositeBands command to arcpy.CompositeBands_management(";".join(rasters), name)

  • Thank you. I tried to run your code and realized that I would like to name new composite raster by the folder name. I updated my question. Please, some more help?
    – maycca
    Dec 28, 2017 at 20:38
  • 1
    After looking at help, I realized that I misunderstood what you were doing. This should make more sense.
    – Bjorn
    Dec 28, 2017 at 20:50

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.