include a file in a python program

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 5 19:12:19 EDT 2010


On Mon, 06 Sep 2010 00:57:30 +0200, bussiere bussiere wrote:

> i've got a python.txt that contain python and it must stay as it
> (python.txt)

Why? Is it against the law to change it? *wink*
 
> how can i include it in my program ?
> import python.txt doesn't work


You could write a custom importer to handle it, but I can't help you with 
that. Try Google.

> is there a way :
> a) to make an include("python.txt")
> b) tell him to treat .txt as .py file that i can make an import python ?

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.



-- 
Steven



More information about the Python-list mailing list