include a file in a python program

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Fri Sep 10 16:50:58 CEST 2010


Steven D'Aprano a écrit :
> 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)

or just:

execfile("python.txt")


> 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.

+10




More information about the Python-list mailing list