tags:

views:

544

answers:

5

How to know, in Python, that the directory you are in is inside a symbolic link ?

I have a directory /tmp/foo/kiwi

I create a symlink /tmp/bar pointing to /tmp/foo

I enter into /tmp/bar/kiwi

the linux command pwd tells me I'm in /tmp/bar/kiwi, which is correct.

The python command prompt tells me I'm in /tmp/foo/kiwi:

Python 2.5.1 (r251:54863, Oct  5 2007, 13:36:32) 
[GCC 4.1.3 20070929 (prerelease) (Ubuntu 4.1.2-16ubuntu2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.getcwd()
'/tmp/foo/kiwi'

Is there a way, in Python, to get the directory I'm really in ?

+3  A: 

If your symlink is set up in the way you state, then /tmp/foo/kiwi is the directory that you're really in. /tmp/bar/kiwi is just another way to get to the same place.

Note that the shell command pwd -P will give you the physical path of the current directory. In your case, the shell is remembering that you got where you are through the bar symlink, so it tell you that you are in /tmp/bar/kiwi.

Greg Hewgill
+4  A: 

If you don't find anything else, you can use

os.getenv("PWD")

It's not really a portable python method, but works on POSIX systems. It gets the value of the PWD environment variable, which is set by the cd command (if you don't use cd -P) to the path name you navigated into (see man cd) before running the python script. That variable is not altered by python, of course. So if you os.chdir somewhere else, that variable will retain its value.

Anyway, as a side node, /tmp/foo/kiwi is the directory you are in. I'm not sure whether anything apart from the shell knows that you've really navigated through another path into that place, actually :)

Johannes Schaub - litb
If you do a os.chdir() -- or whatever equivalent Python has, $PWD, inherited from ther environement won't change...
Keltia
yeah, it's only for those cases where he has that PWD in his environment and want to get its initial value :)
Johannes Schaub - litb
So I'll consider it's not really possible in a portable way. Thank you.
Oli
A: 

Just as a matter of interest, if you are in a directory you can use the -P option to get the pwd command to resolve all symbolic links to their actual directories.

$ ln -s Desktop toto
$ cd toto
$
$ pwd
/home/ken/toto
$ 
$ pwd -P
/home/ken/Desktop
$

HTH

cheers,

Rob

Rob Wells
Note that pwd is a builtin in most shells and generally act as "pwd", not "pwd -P". PS: Hi Rob
Keltia
A: 

You could also try lstat. It will give you info about a file/dir, including telling you whether it's a link and showing you where it links to if it is.

Adam Hawes
os.lstat('.') seems to match os.stat('.') when inside a symlinked directory, so probably not of use to the questioner here.
Paul Stephenson
A: 

When your shell returning the path, it is relying on the shell enviroment variable "PWD" which gets set as you traverse through the symlink path, but actually it is under the directory as returned by the getcwd(). So, if you get from shell's PWD you will get what you want.

>>> os.getcwd()
'/home/ors/foo/tmp/foo/kiwi'
>>> os.environ["PWD"]
'/home/ors/foo/tmp/bar/kiwi'
>>>
Senthil Kumaran