它可能很简单,但我无法在线找到解决方案……
我正在尝试使用存储为netcdf文件的一系列数据集.我打开每一个,读入一些关键点,然后移动到下一个文件.我发现我经常遇到mmap错误/脚本在读入更多文件时变慢.我相信这可能是因为netcdf文件没有被.close()命令正确关闭.
我一直在测试这个:
from scipy.io.netcdf import netcdf_file as ncfile
f=ncfile(netcdf_file,mode='r')
f.close()
如果我试试
>>>f
和
>>>f.variables['temperature'][:]
array([ 1234.68034431, 1387.43136567, 1528.35794546, ..., 3393.91061952,
3378.2844357 , 3433.06715226])
所以看来文件仍然是开放的? close()实际上做了什么?我怎么知道它有效?
有没有办法从python关闭/清除所有打开的文件?
软件:
Python 2.7.6,scipy 0.13.2,netcdf 4.0.1
解决方法:
f.close的代码是:
Definition: f.close(self)
Source:
def close(self):
"""Closes the NetCDF file."""
if not self.fp.closed:
try:
self.flush()
finally:
self.fp.close()