We are trying to make a script that goes through a folder full of unzipped Landsat folders and stacks all the Landsat images it finds. However since Landsat 8 images have more bands then we want to re-order the stacking so that the thermal bands are at the end of the composite stack. The script works for just Landsat 8 images, but we tried to add in a line so that if the number of TIFs in a folder is less than 10 then it just stacks them normally and doesn't re-order them.

The script print out is as follows, which seems to suggest there is a problem with the lists for the non-Landsat 8 images having lists within them.

Stacking Landsat bands together
[u'LC80430202016124LGN00_B1.TIF', u'LC80430202016124LGN00_B2.TIF', u'LC80430202016124LGN00_B3.TIF', u'LC80430202016124LGN00_B4.TIF', u'LC80430202016124LGN00_B5.TIF', u'LC80430202016124LGN00_B6.TIF', u'LC80430202016124LGN00_B7.TIF', u'LC80430202016124LGN00_B8.TIF', u'LC80430202016124LGN00_B10.TIF', u'LC80430202016124LGN00_B11.TIF']
Stacking bands for image D:\Landsat\New\LC80430202016124LGN00.tif
[u'LC81420402016034LGN00_B1.TIF', u'LC81420402016034LGN00_B2.TIF', u'LC81420402016034LGN00_B3.TIF', u'LC81420402016034LGN00_B4.TIF', u'LC81420402016034LGN00_B5.TIF', u'LC81420402016034LGN00_B6.TIF', u'LC81420402016034LGN00_B7.TIF', u'LC81420402016034LGN00_B8.TIF', u'LC81420402016034LGN00_B10.TIF', u'LC81420402016034LGN00_B11.TIF']
Stacking bands for image D:\Landsat\New\LC81420402016034LGN00.tif
[[u'LE71420402002339SGS00_B1.TIF', u'LE71420402002339SGS00_B2.TIF', u'LE71420402002339SGS00_B3.TIF', u'LE71420402002339SGS00_B4.TIF', u'LE71420402002339SGS00_B5.TIF', u'LE71420402002339SGS00_B6_VCID_1.TIF', u'LE71420402002339SGS00_B6_VCID_2.TIF', u'LE71420402002339SGS00_B7.TIF', u'LE71420402002339SGS00_B8.TIF']]
Stacking bands for image D:\Landsat\New\LE71420402002339SGS00.tif
[[u'LM21520401977080AAA05_B4.TIF', u'LM21520401977080AAA05_B5.TIF', u'LM21520401977080AAA05_B6.TIF', u'LM21520401977080AAA05_B7.TIF']]
Stacking bands for image D:\Landsat\New\LM21520401977080AAA05.tif
[[u'LT51410402010362BKT00_B1.TIF', u'LT51410402010362BKT00_B2.TIF', u'LT51410402010362BKT00_B3.TIF', u'LT51410402010362BKT00_B4.TIF', u'LT51410402010362BKT00_B5.TIF', u'LT51410402010362BKT00_B6.TIF', u'LT51410402010362BKT00_B7.TIF']]
Stacking bands for image D:\Landsat\New\LT51410402010362BKT00.tif

The script follows

import arcpy
import os
outws = r"D:\Landsat\New"
arcpy.env.workspace = outws

folders = arcpy.ListWorkspaces()
newrasters_l8 = []
newrasters_l7 = []

for folder in folders:
    arcpy.env.workspace = folder
    rasters = arcpy.ListRasters("*.tif")
    if len(rasters) == 12:
        name_l8 = os.path.join(outws, rasters[1].split("_")[0] + ".tif")
        arcpy.CompositeBands_management(newrasters_l8, name_l8)
    elif len(rasters) < 10:
        print newrasters_l7
        name = os.path.join(outws, rasters[1].split("_")[0] + ".tif")
        print "Stacking bands for image "+name
        arcpy.CompositeBands_management(newrasters_l7, name)
  • I think you should be able to supply noticeably less code than this as a code snippet. What do you mean by "get stuck after the first image"? Is there an error or something else?
    – PolyGeo
    Aug 12, 2016 at 11:23
  • I've now added the error message we get. Aug 15, 2016 at 8:32
  • You have provided us with an error message that refers to line 31 but the code snippet that you have presented has less lines than that so the two do not correspond. Can you please present a code snippet that starts with import arcpy and the error that results when you run that code snippet, please? We will also need to see any output from your print statement.
    – PolyGeo
    Aug 15, 2016 at 8:33
  • You said previously to take away unnecessary part of the code in order to leave as little as possible! I will update the code again Aug 16, 2016 at 6:28
  • You are still presenting an error message that refers to line 31 when the code you present has only 24 lines, and you are still not showing us the output from your print statements, particularly the ones immediately before you see the error message.
    – PolyGeo
    Aug 16, 2016 at 6:58

1 Answer 1


I am not certain, but where you have:


I suspect you want:


or simply

newrasters_17 = rasters  # which is already a list

The former will result in a list of lists instead of a list.

The difference between Python append and extend is explained in https://stackoverflow.com/questions/252703/append-vs-extend


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.