Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建hdf5文件时出现权限错误_Python_Python 3.x_Hdf5_H5py - Fatal编程技术网

Python 创建hdf5文件时出现权限错误

Python 创建hdf5文件时出现权限错误,python,python-3.x,hdf5,h5py,Python,Python 3.x,Hdf5,H5py,我有以下代码段来创建hdf5文件,并使用“with”语句来确保文件正确关闭。然而,我仍然有如下错误消息 filename = 'E30.hdf5' try: with h5py.File(filename, 'w-') as f: print('---') except: os.remove(filename) f = h5py.File(filename, 'w-') 然而,我仍然有如下错

我有以下代码段来创建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'
---------------------------------------------------------------------------
OSError回溯(最近一次调用上次)
在视频至视频hdf中(En、开始、结束、块)
9尝试:
--->10将h5py.File(文件名“w-”)作为f:
11打印('--')
~\AppData\Local\Continuum\anaconda3\envs\fastai-py37\lib\site packages\h5py\\u hl\files.py in uuuuu init_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
407 fapl,fcpl=制造fcpl(轨道顺序=轨道顺序),
-->408 swmr=swmr)
409
make\u fid中的~\AppData\Local\Continuum\anaconda3\envs\fastai-py37\lib\site packages\h5py\\u hl\files.py(名称、模式、用户块大小、fapl、fcpl、swmr)
176['w-','x']中的elif模式:
-->177 fid=h5f.create(名称,h5f.ACC,fapl=fapl,fcpl=fcpl)
178 elif模式=='w':
h5py.\u objects.with\u phil.wrapper()中的h5py\\u objects.pyx
h5py.\u objects.with\u phil.wrapper()中的h5py\\u objects.pyx
h5py.h5f.create()中的h5py\h5f.pyx
OSError:无法创建文件(无法打开文件:名称='E30.hdf5',错误号=17,错误消息='file exists',flags=15,o_flags=502)
在处理上述异常期间,发生了另一个异常:
PermissionError回溯(最近一次调用上次)
在里面
在视频至视频hdf中(En、开始、结束、块)
11打印('--')
12除:
--->13 os.remove(文件名)
14 f=h5py.File(文件名“w-”)
15#在文件中创建数据集
PermissionError:[WinError 32]进程无法访问该文件,因为另一个进程正在使用该文件:“E30.hdf5”

您同时遇到多个问题。 首先,让我们从
h5py.File()
access\u mode标志开始

  • w-:创建文件,如果存在则失败(避免意外覆盖现有文件)
  • w:创建文件,如果存在则截断(表示它覆盖现有文件)
  • r+:读/写,文件必须存在(用于打开现有文件以写入数据)
在下面的逻辑中,如果
E30.hdf5
不存在,您的
try:/except:
模式将执行
try:
语句。如果
E30.hdf5
存在,它将执行
语句,除了:
语句

这由于不同的
h5py而变得复杂。File()
方法适用于每个分支。您的
try:
分支使用带有h5py.File()的
作为f:
方法。因此,当您的代码执行此逻辑时,文件将在最后完全关闭(没有
f.close()
语句)

但是,您的
分支使用
f=h5py.File()
。因此,当您的代码执行此逻辑时,您需要一个
f.close()
语句来确保最后的闭包

这就是我认为您正在经历的情况:

  • 我假设第一次运行代码时,
    E30.hdf5
    不存在
  • 因此,第一次运行时,您将通过
    try:
    分支,文件将在末尾完全关闭
  • 下一次运行代码时,
    E30.hdf5
    存在,因此,您将通过
    除:
    分支。因此,该文件在进程结束时没有关闭,其他进程无法访问它(Python或OS)
  • 编码建议:

    您的
    除了:
    块具有相同的行为
    mode=w
    。下面的代码的行为相同,并且在进程完成时将始终关闭文件。此外,它更具可读性(IMHO)。注意:如果存在,两种方法都会删除
    E30.hdf5

    filename = 'E30.hdf5'
    with h5py.File(filename, 'w') as f: # use mode=w
         print('---')
    
    如果迫切需要保留
    try:/except:
    模式,请进行此更改:(在访问模式
    w-
    r+
    中使用
    try:/except:
    非常有用,无需
    os.remove(文件名)


    它不允许您删除该文件以重试,因为另一个进程正在使用它(可能是另一个python会话)。这似乎是Windows的一个“功能”。您需要找到另一个进程并停止它,然后希望它能够工作。当一个线程试图修改共享资源,而另一个线程正在修改该资源时,会出现争用情况,因此需要同步线程。您可以使用线程模块acquire()和release()方法感谢您的详细解释。
    filename = 'E30.hdf5'
    try:
        with h5py.File(filename, 'w-') as f:
            print('---')
    except: 
        os.remove(filename)
        with h5py.File(filename, 'w-') as f:
             print('+++')