knowing a file's own name

Scott Frankel leknarf at pacbell.net
Mon Nov 29 18:37:44 CET 2004


I'm looking for a way to identify a filename remotely.  Put differently,
is there a way a file can get its own name from its globals()?

doit.py calls exec() on a second py script, tpairs.py, to obtain a dict 
of the
globals in tpairs.py.  How can I add the filename, "tpairs.py," to the 
resulting dict?
i.e.:

# - - - - - - - - - - - - - - - -
# -- tpairs.py
# - - - - - - - - - - - - - - - -
this  = "this"
that  = "that"
gdict = globals()

# - - - - - - - - - - - - - - - -
# -- doit.py
# - - - - - - - - - - - - - - - -
#!/usr/bin/env python

theFile = "./tpairs.py"
theDict = {}
execfile(theFile, theDict)

# somehow add theFile to gdict, i.e.:
# gdict['theFile'] = theFile


Thanks in advance!
Scott




More information about the Python-list mailing list