hon*_*a_p 20
这是如何做到的(我无法弄清楚如何在没有例外的情况下检查文件的封闭性,也许你会发现):
import gc
for obj in gc.get_objects(): # Browse through ALL objects
if isinstance(obj, h5py.File): # Just HDF5 files
try:
obj.close()
except:
pass # Was already closed
Run Code Online (Sandbox Code Playgroud)
另一个想法:
在讨论如何使用文件,使用上下文管理器和这样的with关键字怎么样?
with h5py.File("some_path.h5") as f:
f["data1"] = some_data
Run Code Online (Sandbox Code Playgroud)
当程序流退出with-block时,无论发生什么情况,文件都会关闭,包括异常等.
pytables(h5py使用)跟踪所有打开的文件并提供一种简单的方法来强制关闭所有打开的hdf5文件。
import tables
tables.file._open_files.close_all()
Run Code Online (Sandbox Code Playgroud)
该属性_open_files还有一些有用的方法可以为您提供打开文件的信息和处理程序。
| 归档时间: |
|
| 查看次数: |
13744 次 |
| 最近记录: |