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?
Best Answer
There are some python 'only' rules stated at the 'Python Gotchas' page.
At the beginning they state that:
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)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.:
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:
ctrl-c
(if you're inside a try/except block).