How to find the filename of a script being run when it is executed from a symlink on linux

Posted by Phil Boltt on Stack Overflow See other posts from Stack Overflow or by Phil Boltt
Published on 2010-03-30T21:36:54Z Indexed on 2010/03/30 21:43 UTC
Read the original article Hit count: 580

Filed under:
|
|
|

Hi,

If I have a python script that is executed via a symlink, is there a way that I can find the path to the script rather than the symlink? I've tried using the methods suggested in this question, but they always return the path to the symlink, not the script.

For example, when this is saved as my "/usr/home/philboltt/scripts/test.py" :

#!/usr/bin/python

import sys

print sys.argv[0]
print __file__

and I then create this symlink

ln -s /usr/home/philboltt/scripts/test.py /usr/home/philboltt/test

and execute the script using

/usr/home/philboltt/test

I get the following output:

/usr/home/philboltt/test
/usr/home/philboltt/test

Thanks! Phil

© Stack Overflow or respective owner

Related posts about python

Related posts about filename