如中所述
"Python Gotchas"
,您可以使用打开“异常”
gdal.UseExceptions()
,例如:
from osgeo import gdal
dsrc = gdal.Open('nonexist')
# ... silence
gdal.UseExceptions()
dsrc = gdal.Open('nonexist')
# Traceback (most recent call last):
# File "<interactive input>", line 1, in <module>
# RuntimeError: `nonexist' does not exist in the file system,
# and is not recognised as a supported dataset name.
你可以随时使用
try
except
block获取实际错误消息字符串:
try:
dsrc = gdal.Open('nonexist')
except RuntimeError as e:
print(str(e))
它将打印错误消息:
`文件系统中不存在“nonlist”,
并且未被识别为支持的数据集名称。