Is a python script aware of its stored location path?
Question: 
    /home/bar/foo/test.py:
    

I am trying `test.py` to print `/home/bar/foo` irrespective of from where I
run the script from:

    
    
    import os
    def foo():
      print os.getcwd()
    

test run:

    
    
    [/home/bar $] python /home/bar/foo/test.py        # echoes /home/bar
    [/tmp $] python /home/bar/foo/test.py             # echoes /tmp
    

`os.getcwd()` not the function for the task. How can I get this done
otherwise?


Answer: Try this:

    
    
    import os.path
    p = os.path.abspath(__file__)
    

