I have a folder (with subfolders) containing different raster files (Sentinel-2 images).

I want to import all the bands to python and creater a raster stack to allow further processing (classification, temporal analysis, etc.). So far, I have managed to create a list containing the full path to each raster. I am missing how to create a loop to import each raster and after that create a stack.

Here is the code I am using so far:

import os

# Define path to unziped sentinel-2 image

# Create a list with all the files in the path (and subfolders) which have the pattern _10m.jp2
rasterFiles = [os.path.join(root, name)
             for root, dirs, files in os.walk(path)
             for name in files
             if name.endswith("_10m.jp2")]


I am trying something like:

for i in rasterFiles():
    gdal.Open(rasterFiles[i], GA_ReadOnly)

TypeError: list indices must be integers, not str

Your Answer

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

Browse other questions tagged or ask your own question.