title: Corrupt files when creating HDF5 files without closing them (h5py)
body: <p>I am using h5py to store experiment data in an HDF5 container.</p>
  
  <p>In an interactive session I open the file using:</p>
  
  <pre><code>measurement_data = h5py.File('example.hdf5', 'a')
  </code></pre>
  
  <p>Then I write data to the file using some self-written functions (can be many GB of data from a couple of days experiment). At the end of the experiment I usually would close the file using</p>
  
  <pre><code>measurement_data.close()
  </code></pre>
  
  <p>Unfortunately, from time to time it happens, that the interactive session ends without me explicitly closing the file (accidentally killing the session, power outage, crash of OS due to some other software). This always results in a corrupt file and loss of the complete data. When I try to open it, I get the error:</p>
  
  <pre><code>OSError: Unable to open file (File signature not found)
  </code></pre>
  
  <p>I also cannot open the file in HDFview, or any other software I tried.</p>
  
  <ol>
  <li>Is there a way to avoid a corrupt file even if it is not closed explicitly? I've read about using the with statement <a href="https://stackoverflow.com/questions/26613570/hdf5-file-created-with-h5py-cant-be-opened-by-h5py">here</a>, but I'm not sure if this would help, when the session unexpectedly ends.</li>
  <li>Can I restore the data in the corrupt files in some way? Is there a repair program available?</li>
  </ol>
  
  <p>Always opening and closing the file for every write access sounds pretty unfavorable to me, because I am continuously writing data from many different functions and threads. So I'd be more happy with a different solution.</p>