Adding a __filename__ predefined attribute to 2.5?

Fredrik Lundh fredrik at pythonware.com
Wed Oct 12 11:31:46 CEST 2005


Rune Strand wrote:

> I know it's several ways to isolate the filename. I just want to avoid
> the overhead of importing sys or os  to achieve it.

those modules are already imported when Python gets to your code, so
the only "overhead" you're saving is a little typing.

> Currently I have this in my scripts:
> __filename__ = __file__.replace('\\', '/').rsplit('/', 1)[-1]

wow.  that's one lousy optimization...

here's a *shorter* piece of code, which is also readable and portable, and
a lot easier to type on most keyboards:

   import os
   __filename__ = os.path.basename(__file__)

</F> 






More information about the Python-list mailing list