According to the GDAL Python Documentation, GDAL functions do not raise exceptions or errors but do print errors to the console via stdout and stderr.

I have some code in my script that calls GetStatistics on a GDAL raster band. If the band only contains nodata cells (which is possible because it is a tile of a larger dataset) the GetStatistics call fails with an error, but the script continues. I would prefer not to see the error messages that are still printed to the console when this happens as it interferes with other print statements that I have written into my own script.

Is it possible to turn off these messages?

  • Can you post some sample code and error printout example? Commented Oct 4, 2013 at 22:59
  • 1
    How about just redirecting stderr (and stdout)? Not GDAL specific - plenty of questions on various stackexchange sites about that.
    – BradHards
    Commented Oct 5, 2013 at 3:49

2 Answers 2


You can turn off the error messages completely with gdal.PushErrorHandler('CPLQuietErrorHandler').

You could combine this with gdal.UseExceptions() as per @nickves' answer so you could log to a file. For example:

import logging
from osgeo import gdal


                    format='%(levelname)s:%(asctime)s %(message)s',

    #raise a GDAL error on purpose
    gdal.Error(gdal.CE_Failure, 1, "Some error message")

If you want some more advanced error handling/logging, you can pass a custom error function with gdal.PushErrorHandler(mycustomfunction).


There are some python 'only' rules stated at the 'Python Gotchas' page.

At the beginning they state that:

By default, the GDAL and OGR Python bindings do not raise exceptions when errors occur. Instead they return an error value such as None and write an error message to sys.stdout.

So first enable the use of exceptions, by issuing gdal.UseExceptions() somewhere in the beginning of your script. And secondly catch if any exceptions and do whatever you want with them (including nothing)

gdal.UseExceptions()  # Enable errors
except RuntimeError:  # <- Check first what exception is being thrown

If you're curious here's a link from python's wiki where they describe the methods of exception handling.

As far as I can tell, using gdal.UseExceptions() will normalize the behaviour of all the methods in the gdal lib, by making those methods to use the Python Exceptions. If you really want to ignore all exceptions put the problematic part of your code inside a try/except block eg.:

    print "1"
    print "2"
    a = 0
    b = 1
    c = b/a   # <- I just divided by zero.
    print "Everything is ok" # <-  but Everything is Ok. 
                             # If you really want to be silent replace with pass
>>> 1
    'Nothing to see here'

Just be careful, because that way when an exception happens, your script will silently break from the procedure without any indication of what went wrong.

Also I want to point a couple of things as well:

  1. If you're opt to go that way, you're responsible if your program does. You're choosing to turn a blind eye at any errors that come in your way.
  2. If you catch ALL the exceptions, you will also catch the 'KeyboardInterrupt' exception as well. No more ctrl-c (if you're inside a try/except block).
  3. Your code at some point is encountering a processing error. Since you're not using gdal.UseExceptions() you're experiencing that error as a sys.stdout response. Use a try/except block at that point to plug that hole.
  • I read the Python Gotchas and saw that statement. I like that GDAL does not throw exceptions, but I would also like it if it did not write the error message to sys.stdout. What's not clear is if I call gdal.UseExceptions() will it then disable the messages to sys.stdout or will it both raise exceptions and provide the message?
    – Brian
    Commented Oct 7, 2013 at 15:00
  • Some methods complaining using sys.stdout, Others silently fail, for example gdal.Open if you point it to a non-existant file. As far as i can tell, with gdal.UseExceptions() ALL the methods will raise an exception if something goes nasty, which is desirable. If you really want to ignore all errors, I've updated the answer on how you could do it.
    – nickves
    Commented Oct 7, 2013 at 17:58
  • Is there a way to switch off exceptions, after enabling it? Commented Jul 11, 2017 at 6:10

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.