Loading files into variables in python

2019-07-17 20:18发布

问题:

I am trying to write a small function that gets a variable name, check if it exists, and if not loads it from a file (using pickle) to the global namespace.

I tried using this in a file:

import cPickle

#
# Load if neccesary
#
def loadfile(variable, filename):
    if variable not in globals():
        cmd = "%s = cPickle.load(file('%s','r'))" % (variable, filename)
        print cmd
        exec(cmd) in globals()

But it doesn't work - the variable don't get defined. What am I doing wrong?

回答1:

Using 'globals' has the problem that it only works for the current module. Rather than passing 'globals' around, a better way is to use the 'setattr' builtin directly on a namespace. This means you can then reuse the function on instances as well as modules.

import cPickle

#
# Load if neccesary
#
def loadfile(variable, filename, namespace=None):
    if module is None:
        import __main__ as namespace
    setattr(namespace, variable, cPickle.load(file(filename,'r')))

# From the main script just do:
loadfile('myvar','myfilename')

# To set the variable in module 'mymodule':
import mymodule
...
loadfile('myvar', 'myfilename', mymodule)

Be careful about the module name: the main script is always a module main. If you are running script.py and do 'import script' you'll get a separate copy of your code which is usually not what you want.



回答2:

You could alway avoid exec entirely:


import cPickle

#
# Load if neccesary
#
def loadfile(variable, filename):
    g=globals()
    if variable not in g:
        g[variable]=cPickle.load(file(filename,'r'))


EDIT: of course that only loads the globals into the current module's globals.

If you want to load the stuff into the globals of another module you'd be best to pass in them in as a parameter:


import cPickle

#
# Load if neccesary
#
def loadfile(variable, filename, g=None):
    if g is None:
        g=globals()
    if variable not in g:
        g[variable]=cPickle.load(file(filename,'r'))

# then in another module do this
loadfile('myvar','myfilename',globals())