knowing a file's own name

Scott Frankel leknarf at pacbell.net
Mon Nov 29 18:53:50 EST 2004


Thanks for the responses!

I'd forgotten about using the sys module:
	import sys
	filename = sys.argv[0]

Using "__file__" also works.

Thanks
Scott


On Nov 29, 2004, at 9:37 AM, Scott Frankel wrote:

>
> 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
>
> -- 
> http://mail.python.org/mailman/listinfo/python-list
>




More information about the Python-list mailing list