title: PermissionError: [WinError 32] for creating files
body: <p>There is a code segment as follows</p>
  <pre><code>try:
      f = h5py.File(filename, 'w-')
  except: 
      os.remove(filename)
      f = h5py.File(filename, 'w-')  
  </code></pre>
  <p>Running the program getting the error related to the above code segment. I think it is because the file was not closed. Googling the similar error message, it seems that it can be solved <a href="https://stackoverflow.com/questions/27215462/permissionerror-winerror-32-the-process-cannot-access-the-file-because-it-is">using &quot;with&quot; statement</a>, but I am not sure how to modify the above code segment.</p>
  <pre><code>OSError                                   Traceback (most recent call last)
  &lt;ipython-input-19-1f1f9c5eb3dd&gt; in vid_to_hdf(En, start, end, chunk)
        9     try:
  ---&gt; 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),
  --&gt; 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']:
  --&gt; 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)
  &lt;timed eval&gt; in &lt;module&gt;
  
  &lt;ipython-input-19-1f1f9c5eb3dd&gt; in vid_to_hdf(En, start, end, chunk)
       10         f = h5py.File(filename, 'w-')
       11     except:
  ---&gt; 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'
  </code></pre>