[Tutor] querying the name of a calling python file
Alan Gauld
alan.gauld at btinternet.com
Fri Nov 5 19:34:44 CET 2010
"Garry Willgoose" <garry.willgoose at newcastle.edu.au> wrote
> For a while now I have been using the command below in a python file
> to determine the name of the python file that I use to launch an
> application
>
> sys.modules['__main__'].__dict__['__file__']
The usual way to do that would be via sys.argv[0]
Is there any reason why you are using the method above?
Any time you find yourself using __xxx__ type names it suggests that
you are getting in too deep! :-) There is usually a simpler
alternative.
If I see three double-underscores in one line I know I'm either
delving
in very deep or missing a trick...
> Is the way I'm getting the file name the recommended way or did
> I pick up a bit of flaky advice?
There may be cases where it will work and argv[0] won't but until
you prove that you need it I'd go with argv[]
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list