Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7061265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:27:10+00:00 2026-05-28T04:27:10+00:00

I wanted to use the following code from here: How can I save all

  • 0

I wanted to use the following code from here:
How can I save all the variables in the current python session?

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()

But it gives the following error:

Traceback (most recent call last):
  File "./bingo.py", line 204, in <module>
    menu()
  File "./bingo.py", line 67, in menu
    my_shelf[key] = globals()[key]
KeyError: 'filename'

Can you help me please?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T04:27:11+00:00Added an answer on May 28, 2026 at 4:27 am

    From your traceback, it appears you are trying to run that code from inside a function.

    But dir looks up names in the current local scope. So if filename is defined inside the function, it will be in locals() rather than globals().

    You probably want something more like this:

    import shelve
    
    T = 'Hiya'
    val = [1, 2, 3]
    
    def save_variables(globals_=None):
        if globals_ is None:
            globals_ = globals()
        filename = '/tmp/shelve.out'
        my_shelf = shelve.open(filename, 'n')
        for key, value in globals_.items():
            if not key.startswith('__'):
                try:
                    my_shelf[key] = value
                except Exception:
                    print('ERROR shelving: "%s"' % key)
                else:
                    print('shelved: "%s"' % key)
        my_shelf.close()
    
    save_variables()
    

    Note that when globals() is called from within the function, it returns the variables from the module where the function is defined, not from where it’s called.

    So if the save_variables function is imported, and you want the variables from the current module, then do:

    save_variables(globals())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following my previous question, in which I wanted to dump all the variables in
I wanted to use boost::thread in my program, but get the following compiler error
I wanted to use one of the Git-completion.bash features but I can't customize the
I wanted to use a python equivalent to piping some shell commands in perl.
When running the following code, the filenames of all files below C:\Test are printed.
I have a DataSet populated from Excel Sheet. I wanted to use SQLBulk Copy
I use the following code to create a public static array in C# public
I am using the following code to initialize a model from within my controller:
How do you set a custom MarkupExtension from code? You can easily set if
I wanted to use T4 to generate properties for a partial class. I'm running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.