[Baypiggies] Can you get the filename which is being execfile()'d from within the file being execfiled?

William Deegan bdbaddog at gmail.com
Thu Dec 13 20:01:57 CET 2007


All,
O.k. thanks for all the help. Here's sample files and output:

--------execfile_slave.py-----------------
# I'm the slave
import sys

print "BILL    =%s"%BILL
print "sys.argv=%s"%sys.argv[0]
print "exfile  =%s"%exfile
print "blah    =%s"%blah
print "file    =%s"%file
print "sys._getframe().f_code.co_filename =
%s"%sys._getframe().f_code.co_filename

# syntax error .. print "__file__=%s\n"%mod.__file__


----------execfile_master.py-------------
#!/usr/bin/python

globals_dict = {
    'exfile'       : 'execfile_slave.py',
    'BILL'       : 'MYName',
    }

globals_dict['blah']='abcd';
print globals_dict
execfile('execfile_slave.py',globals_dict)

Running via: python execfile_master.py yields:
> ./execfile_master.py
{'blah': 'abcd', 'BILL': 'MYName', 'exfile': 'execfile_slave.py'}
BILL    =MYName
sys.argv=./execfile_master.py
exfile  =execfile_slave.py
blah    =abcd
file    =<type 'file'>
sys._getframe().f_code.co_filename = execfile_slave.py


So it seems the working solutions are:
sys._getframe().f_code.co_filename
or
pass a global in the global_dict with the filename being sourced.

Thanks again for all the help!
As usual Baypiggies comes through.
Now if I could just make it to a dang meeting.
;)

-Bill
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/baypiggies/attachments/20071213/6d2fe7df/attachment.htm 


More information about the Baypiggies mailing list