【发布时间】:2020-12-04 22:15:47
【问题描述】:
我有以下代码段来创建一个 hdf5 文件,并使用“with”语句来确保文件正确关闭。但是,我仍然收到如下错误消息。
filename = 'E30.hdf5'
try:
with h5py.File(filename, 'w-') as f:
print('---')
except:
os.remove(filename)
f = h5py.File(filename, 'w-')
但是,我仍然收到如下错误消息。在工作目录中,可能已经有一个名为“E30.hdf5”的现有文件。但这真的重要吗?我试图直接从Windows中删除它。但是,windows不允许我删除它说它正在打开。
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-6-e8ccfbc1b5d2> in vid_to_hdf(En, start, end, chunk)
9 try:
---> 10 with h5py.File(filename, 'w-') as f:
11 print('---')
~\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 (unable to open file: name = 'E30.hdf5', errno = 17, error message = 'File exists', flags = 15, o_flags = 502)
During handling of the above exception, another exception occurred:
PermissionError Traceback (most recent call last)
<timed eval> in <module>
<ipython-input-6-e8ccfbc1b5d2> in vid_to_hdf(En, start, end, chunk)
11 print('---')
12 except:
---> 13 os.remove(filename)
14 f = h5py.File(filename, 'w-')
15 # Create dataset within file
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'E30.hdf5'
【问题讨论】:
-
它不允许您删除文件以重试,因为另一个进程正在使用它(可能是另一个 python 会话)。这似乎是 Windows 的一个“功能”。您需要找到另一个进程并停止它,然后希望它应该可以工作。
-
当一个线程尝试修改共享资源的同时另一个线程正在修改该资源时,会发生争用情况,线程需要同步。您可以利用线程模块的 acquire() 和 release() 方法
标签: python python-3.x hdf5 h5py