对 ogr.open(file) 使用 Python "with" 语句
Use Python "with" statement for ogr.open(file)
是否可以将 Python "with" 语句与 ogr.open(file) 函数一起使用?
例如,我想做这样的事情:
with ogr.open(file) as ds:
目前,我只能使用以下方法:
try:
ds = ogr.open(file)
...
except:
del ds
根据文档,您似乎无法使用 with ogr.Open(file)
...
您在 with
语句中使用的 Python 对象需要有方法 __enter__
和 __exit__
来设置和取消 with
中使用的上下文=11=]块。 Here's an explanation.
根据 Documentation for OGR Open,这些 __enter__
和 __exit__
方法没有为 Open
返回的 DataSource 对象定义,因此您不能使用 ogr.Open
作为 with
语句的主题。
看来您必须使用 try
/except
组合(尽管 try
/finally
组合可能更好)。
是否可以将 Python "with" 语句与 ogr.open(file) 函数一起使用?
例如,我想做这样的事情:
with ogr.open(file) as ds:
目前,我只能使用以下方法:
try:
ds = ogr.open(file)
...
except:
del ds
根据文档,您似乎无法使用 with ogr.Open(file)
...
您在 with
语句中使用的 Python 对象需要有方法 __enter__
和 __exit__
来设置和取消 with
中使用的上下文=11=]块。 Here's an explanation.
根据 Documentation for OGR Open,这些 __enter__
和 __exit__
方法没有为 Open
返回的 DataSource 对象定义,因此您不能使用 ogr.Open
作为 with
语句的主题。
看来您必须使用 try
/except
组合(尽管 try
/finally
组合可能更好)。