include a file in a python program

  • Thread starter bussiere bussiere
  • Start date
B

bussiere bussiere

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

how can i include it in my program ?
import python.txt doesn't work
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 ?
i'am using python3
Regards
Bussiere
fan of torchwood
Google Fan boy
 
S

Steven D'Aprano

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

Roy Smith

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

how can i include it in my program ?
import python.txt doesn't work
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 ?
i'am using python3

The simple solution (at least for unix-ish systems) would be to make a
symlink python.py -> python.txt. If you don't have the ability to do
that externally, you could even have your python program create the
symlink on the fly, import the module, then delete the symlink. The
symlink could even be in /tmp.

Another possible solution is to read the entire file into a string and
then eval() the string. I'm assuming eval() still exists in python 3;
does it?

Yet another possibility (never tried this, but it seems reasonable)
would be to compile() your text file
(http://docs.python.org/py3k/library/functions.html#compile).

Of course, it would be a lot easier if you just renamed the file, but
I'll take it as a given that there are external forces which prevent you
from doing that.
 
R

Roy Smith

Steven D'Aprano said:
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.

Well, this is true, but eval() or exec() isn't really exposing him to
anything that import isn't.
 
T

Terry Reedy

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

Niklasro(.appspot)

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

how can i include it in my program ?
import python.txt doesn't work
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 ?
i'am using python3
Regards
Bussiere
fan of torchwood
Google Fan boy

You can do it with tkinter to also enable GUI.
 
B

bussiere maillist

Thanks all for your response i will try out this week, you have give
me sufficient hint.
Thanks again.
Bussiere
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top