title: How to properly open, read from and save to a single file in python 3 using H5py
body: <p>I am new to python and programming in gerneral and am probably making horrible mistakes. Thank you for any help. I want to to initialize a member of my class by loading either some hdf5 data prepared by someone else or by loading my own hdf5 files. I tried this:</p>
  
  <pre><code>import numpy as np
  import h5py
  import sys
  
  class ashot:
      def __init__(self, path, load=False):
          if load is False:
              self.name = "_".join(re.findall(r"(\d+)_(\d+)/aa/shot_(\d+)", path)[0])
              f = h5py.File(path, "r")
              numpyarray = f["data/data"]
              self.array = numpyarray
          else:
              f = h5py.File(path, "a")
              self.array = f["array"]
              self.name = f["array"].attrs["name"]
  
      def saveshot(self):
          s = h5py.File(self.name+".h5", "a")
          s.create_dataset("array", data=self.array)
          s["array"].attrs["name"] = self.name
          s.close()
          return()
  </code></pre>
  
  <p>but if I run it using:</p>
  
  <pre><code>testshot = ashot("somepath to data storage")
  testshot.saveshot()
  loadshot = ashot("the path I stored the shot testshot", load = True)
  loadshot.saveshot()
  </code></pre>
  
  <p>I get</p>
  
  <pre><code>Traceback (most recent call last):
  File "program path.py", line 191, in &lt;module&gt;
  loadshot.saveshot()
  File "program path.py", line 114, in saveshot
  s.create_dataset("array", data=self.array)
  File "C:\Users\Drossel\AppData\Local\Programs\Python\Python36\lib\site-packages\h5py\_hl\group.py", line 109, in create_dataset
  self[name] = dset
  File "C:\Users\Drossel\AppData\Local\Programs\Python\Python36\lib\site-packages\h5py\_hl\group.py", line 277, in __setitem__
  h5o.link(obj.id, self.id, name, lcpl=lcpl, lapl=self._lapl
  File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper
  File "h5py\_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
  File "h5py\h5o.pyx", line 202, in h5py.h5o.link
  RuntimeError: Unable to create link (name already exists)
  </code></pre>
  
  <p>I kinda get that I am trying to write to a file that is already open, but the same code usoing numpy.save and numpy.load works for some reason. I tried closing the file after assiningthe self.array, but then i get </p>
  
  <pre><code>NameError: name 'ashot' is not defined
  </code></pre>
  
  <p>Beacuse, I'm assuming, f is only a file handle at that point. What am I doing wrong? </p>