include a file in a python program

Terry Reedy tjreedy at udel.edu
Sun Sep 5 19:41:39 EDT 2010


On 9/5/2010 7:12 PM, Steven D'Aprano wrote:

> fp = open("python.txt")
> text = fp.read()
> fp.close()
> exec(text)
>
> But keep in mind that the contents of python.txt will be executed as if
> you had typed it yourself. If you don't trust the source with your life
> (or at least with the contents of your computer), don't execute it.

Also, this executes the code within the namespace of the calling code 
rather than within a fresh module namespace. That could be either better 
or worse.

-- 
Terry Jan Reedy




More information about the Python-list mailing list