在numpy.load()
的docstring中,我发现了以下警告:
For
.npz
files, the returned instance of NpzFile class must be closed to avoid leaking file descriptors.
我注意到,返回的NpzFile
对象同时具有__enter__()
和__exit__()
方法。你知道吗
如果我这样使用它,它会自动关闭吗:
>>> with numpy.load('my_mile.npz') as data:
... A = data['A']
什么?你知道吗
是的。使用
with
语句将关闭类似文件的对象。下面是一个直接来自the documentation的示例:相关问题 更多 >
编程相关推荐