problem with linked directory

Frank Tobin ftobin at neverending.org
Tue May 7 15:40:00 EDT 2002


SungPil Yoon, on 2002-05-07, wrote:

>     /home/userdir/dir3 -> /work/userdir/dir3
...
>     /home/userdir/dir3> pwd
>     /home/userdir/dir3

> But os.getcwd function does not work in the way that I hope:
>
>     >>> import os
>     >>> os.getcwd()
>     /work/userdir/dir3

You shouldn't be expecting os.getcwd() to give you the linkname.  Think
about it this way; it doesn't matter how you 'got' to /work/userdir/dir3,
whether it be through the symlink or not; getcwd() is only guaranteed to
return an absolute pathname of the current working directory, which is not
necessarily unique.

The only reason that the shell's built-in pwd does what you want is
because the shell is keeping around the trail of how you got to the
directory (e.g., following the symlink).  If you ran /bin/pwd I think
you'll find that you get the same result as you did with os.getcwd().

This is not a Python issue; the getcwd(3) library call will do the same.

-- 
Frank Tobin		http://www.neverending.org/~ftobin/




More information about the Python-list mailing list