【发布时间】:2020-12-04 00:52:12
【问题描述】:
有一段代码如下
try:
f = h5py.File(filename, 'w-')
except:
os.remove(filename)
f = h5py.File(filename, 'w-')
运行程序得到与上述代码段相关的错误。我认为这是因为文件没有关闭。 google了一下类似的错误信息,貌似可以解决using "with" statement,但是不知道上面代码段怎么修改。
OSError Traceback (most recent call last)
<ipython-input-19-1f1f9c5eb3dd> in vid_to_hdf(En, start, end, chunk)
9 try:
---> 10 f = h5py.File(filename, 'w-')
11 except:
~\AppData\Local\Continuum\anaconda3\envs\fastai-py37\lib\site-packages\h5py\_hl\files.py in __init__(self, name, mode, driver, libver, userblock_size, swmr, rdcc_nslots, rdcc_nbytes, rdcc_w0, track_order, **kwds)
407 fapl, fcpl=make_fcpl(track_order=track_order),
--> 408 swmr=swmr)
409
~\AppData\Local\Continuum\anaconda3\envs\fastai-py37\lib\site-packages\h5py\_hl\files.py in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
176 elif mode in ['w-', 'x']:
--> 177 fid = h5f.create(name, h5f.ACC_EXCL, fapl=fapl, fcpl=fcpl)
178 elif mode == 'w':
h5py\_objects.pyx in h5py._objects.with_phil.wrapper()
h5py\_objects.pyx in h5py._objects.with_phil.wrapper()
h5py\h5f.pyx in h5py.h5f.create()
OSError: Unable to create file (file exists)
During handling of the above exception, another exception occurred:
PermissionError Traceback (most recent call last)
<timed eval> in <module>
<ipython-input-19-1f1f9c5eb3dd> in vid_to_hdf(En, start, end, chunk)
10 f = h5py.File(filename, 'w-')
11 except:
---> 12 os.remove(filename)
13 f = h5py.File(filename, 'w-')
14 # Create dataset within file
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'E23.hdf5'
【问题讨论】:
标签: python python-3.x h5py