0

the __FILE__ is a convenient macro designed to access current FILE path in python. But there's some issue with it when I use it. I have a file say /home/a/b.py so when accessed from directory a the __FILE__ macro in b.py should give /home/a/b.py which is correct. But I then created a symbol link under /var/www/a -> /hom/a so when the b.py is accessed from /var/www/a I would expect macro in b.py to give the path /var/www/a/b.py. But this is not the case. although the file is accessed form symbol path /var/www/a, the macro is still giving the real file path /home/a/b.py

I've tried abspath and realpath in python os package. is there any api that can peel out the symbolic path I wanted?

zinking
  • 5,561
  • 5
  • 49
  • 81
  • Try hard links maybe? Otherwise you could set an environment variable with the configured path. – djhoese Mar 10 '14 at 01:50
  • 1
    related: [How do I get the path of the current executed file in python?](http://stackoverflow.com/q/2632199/4279) – jfs Mar 10 '14 at 01:55
  • 1
    `__file__` is not a macro (note: lowercase). It is an ordinary variable that is typically set by a module loader. Symlinks are not the only gotchas, see the link above. – jfs Mar 10 '14 at 01:57
  • @J.F.Sebastian , I am a bit confused, so this is not possible? the b.py is loaded from module /var/www/a , so it should print `/var/www/a` – zinking Mar 10 '14 at 02:24
  • @zinking: [follow the link](http://stackoverflow.com/q/2632199/4279) – jfs Mar 10 '14 at 02:25
  • @J.F.Sebastian can you put it as an answer, so that I can accept ? – zinking Apr 04 '14 at 02:04
  • @zinking: if you think you know the answer; you could [provide your own answer](http://stackoverflow.com/help/self-answer) – jfs Apr 04 '14 at 15:17

1 Answers1

0

You can use the first argument from the command line if the script is called with the full path. Here's an example I just ran:

import sys
print(sys.argv)

From the command line:

python test.py
# results in ['test.py']
python /full/path/test.py
# results in ['/full/path/test.py']
djhoese
  • 3,567
  • 1
  • 27
  • 45