[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

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

# 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 =

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


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

print globals_dict

Running via: python execfile_master.py yields:
> ./execfile_master.py
{'blah': 'abcd', 'BILL': 'MYName', 'exfile': 'execfile_slave.py'}
BILL    =MYName
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:
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.

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