title: Shelve Code gives KeyError
body: <p>I wanted to use the following code from here:
  <a href="https://stackoverflow.com/questions/2960864/how-can-i-save-all-the-variables-in-the-current-python-session">How can I save all the variables in the current python session?</a></p>
  
  <pre><code>import shelve
  
  T='Hiya'
  val=[1,2,3]
  
  filename='/tmp/shelve.out'
  my_shelf = shelve.open(filename,'n') # 'n' for new
  
  for key in dir():
      try:
          my_shelf[key] = globals()[key]
      except TypeError:
          #
          # __builtins__, my_shelf, and imported modules can not be shelved.
          #
          print('ERROR shelving: {0}'.format(key))
  my_shelf.close()
  </code></pre>
  
  <p>But it gives the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "./bingo.py", line 204, in &lt;module&gt;
      menu()
    File "./bingo.py", line 67, in menu
      my_shelf[key] = globals()[key]
  KeyError: 'filename'
  </code></pre>
  
  <p>Can you help me please?</p>
  
  <p>Thanks!</p>