5

I'm using GDAL to calculate NDVI mean and max and to find other information about .tif Landsat images acquired from Earth Explorer. I stacked the bands into a single .tif image and have a working python script to find everything I want to know about the image, however I have many .tif files and I don't want to run the script individually for each file, and I want to make my script automatically run the code over every .tif file I have in the same folder.

The beginning of my script, and how I am accessing the single .tif file looks like this:

from __future__ import print_function

from osgeo import gdal

print("GDAL's version is: " + gdal.__version__)

print (gdal)

print(gdal.GDT_Byte)

dataset = gdal.Open('/Path/To/Folder/Containing/.tif', gdal.GA_ReadOnly)

How would I get Python to run the script over every file in this folder automatically?

1 Answer 1

7

The python module glob is used to get all file names according to the pattern that you give it. The documentation for glob can be found here.

Within your script you would use glob like this:

in_directory = r'C:\Data'
files_to_process = glob.glob(os.path.join(in_directory, '*.tif')
for data_path in files_to_process:
    raster_dataset = gdal.Open(data_path, gdal.GA_ReadOnly)
    #do your processing on the raster dataset here

In the snippet above the * is a wildcard to match any string and the .tif is static, so this will match all files that end with .tif. glob.glob returns a list of all files that match your criteria. You can then loop over each filename in the list returned by glob. In addition to wildcard matching you can also use regular expressions. glob is a very useful module for batch processing and is part of the Python Standard Library, e.g. it is automatically shipped with Python.

2
  • getting error: num_bands = dataset.RasterCount AttributeError: 'list' object has no attribute 'RasterCount' here is the start of my code: from future import print_function from osgeo import gdal import glob import os print("GDAL's version is: " + gdal.__version__) print (gdal) print(gdal.GDT_Byte) files = r'/Path/To/Folder/With/.tif/Files' dataset = glob.glob(os.path.join(files, '*.tif')) for data in dataset: print (dataset) num_bands = dataset.RasterCount
    – Vanludvig
    Nov 20, 2016 at 21:49
  • 2
    @user71356 the variable files_to_process is a list of the file names. You still need to run gdal.Open() on the file name, in my example the variable data_path just like you did in your original question. I updated the answer to demonstrate this. In your code above you are calling .RasterCount on a list object, just like the AttributeError says a 'list' object has no attribute 'RasterCount'
    – GeoSharp
    Nov 21, 2016 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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