Python 3: exec arg 1

Alan G Isaac alan.isaac at
Sat Jan 17 20:12:45 CET 2009

In Python 3, you can no longer ``exec(open(filename))``.

I guess the reason is that in 3.0 ``open`` returns a stream,
instead of open file, and exec wants
"a string, bytes, or code object" and not a "TextIOWrapper".
So it returns an error.

Is it intentional that ``exec`` cannot handle a TextIOWrapper?

Bottom line: has ``execfile(filename)`` really become
``exec(open(filename).read())``? Is this a good thing?

Alan Isaac

More information about the Python-list mailing list