Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python“;加上;ogr.open(文件)的语句_Python_File_Geospatial_Ogr - Fatal编程技术网

使用Python“;加上;ogr.open(文件)的语句

使用Python“;加上;ogr.open(文件)的语句,python,file,geospatial,ogr,Python,File,Geospatial,Ogr,可以将Python语句与ogr.open(file)函数一起使用吗 例如,我想做如下事情: with ogr.open(file) as ds: 目前,我只能做到以下几点: try: ds = ogr.open(file) ... except: del ds 根据文档,您似乎无法将与ogr.Open(file)一起使用 在with语句中使用的Python对象需要具有方法\uuuuuu enter\uuuu和\uuuuuu exit\uuuuu来设置和删除with块

可以将Python语句与ogr.open(file)函数一起使用吗

例如,我想做如下事情:

with ogr.open(file) as ds:
目前,我只能做到以下几点:

try:
    ds = ogr.open(file)
    ...

except:
    del ds

根据文档,您似乎无法将
与ogr.Open(file)
一起使用

with
语句中使用的Python对象需要具有方法
\uuuuuu enter\uuuu
\uuuuuu exit\uuuuu
来设置和删除
with
块中使用的上下文

根据,这些
\uuuuuuuuuuuuuuuuu
\uuuuuuuuuu
方法没有为
Open
返回的数据源对象定义,因此不能将
ogr.Open
的结果用作
with
语句的主题


因此,看起来您必须使用您的
尝试
/
组合,但不使用
组合(尽管
尝试
/
最终
组合可能会更好)。

您希望如何使用
组合?在末尾调用
ds.close()
?ds(数据源)对象没有close()属性。如果不使用try-finally组合(或try-except),我发现我的解释器会锁定打开的ds(数据源),即使脚本中有“del-ds”。谢谢,您的解释帮助我理解了为什么我不能使用它。这将有助于防止软件在脚本执行后对文件保持锁定。