What's so wrong about execfile?

Michele Simionato michele.simionato at gmail.com
Sun Mar 1 07:34:47 CET 2009


On Feb 28, 4:21 am, Sammo <sammo2... at gmail.com> wrote:
> Given that execfile has been removed in py3k

execfile has not been really removed, it is just spelled
differently:

>>> exec(open(myfile.py).read())

BTW, from the help message

>>> help(exec)
Help on built-in function exec in module builtins:

exec(...)
    exec(object[, globals[, locals]])

    Read and execute code from a object, which can be a string, a code
    object or a file object.
    The globals and locals are dictionaries, defaulting to the current
    globals and locals.  If only globals is given, locals defaults to
it.

I would have expected

>>> exec(open('myfile.py'))

to work too. Any idea why it does not?


                 Michele Simionato



More information about the Python-list mailing list