__FILE__ equivalent in Python; get the path to current file in Python

Update:
There is __file__ in Python.

You could do this as suggested by some but I don’t know why:

import inspect
this_file = inspect.currentframe().f_code.co_filename

I hope this helps :)

4 Comments for __FILE__ equivalent in Python; get the path to current file in Python

  1. Adam Gomaa said,

    November 25, 2008 @ 2:11 pm

    It’s __file__ in Python.

  2. Codehead said,

    November 25, 2008 @ 2:20 pm

    Hey, I know now, it’s very silly but I was searching for it and this was what I found!

  3. nancy said,

    January 5, 2009 @ 8:22 pm

    The problem with __file__ is that it doesn’t always work. Using the inspect module is more robust.

  4. Codehead said,

    January 5, 2009 @ 8:37 pm

    Hey, thanks for the info, it would be nice if you could explain this a little more though. Thanks!

RSS comments feed· TrackBack URI __FILE__ equivalent in Python; get the path to current file in Python

Leave a Comment for __FILE__ equivalent in Python; get the path to current file in Python