Suppose I have a path named:
/this/is/a/real/path
Now, I create a symbolic link for it:
/this/is/a/link -> /this/is/a/real/path
and then, I put a file into this path:
/this/is/a/real/path/file.txt
and cd it with symbolic path name:
cd /this/is/a/link
now, pwd command will return the link name:
> pwd
/this/is/a/link
and now, I want to get the absolute path of file.txt as:
/this/is/a/link/file.txt
but with python's os.abspath()
or os.realpath()
, they all return the real path (/this/is/a/real/path/file.txt
), which is not what I want.
I also tried subprocess.Popen('pwd')
and sh.pwd()
, but also get the real path instead of symlink path.
How can I get the symbolic absolute path with python?
Update
OK, I read the source code of pwd
so I get the answer.
It's quite simple: just get the PWD
environment variable.
This is my own abspath
to satify my requirement:
def abspath(p):
curr_path = os.environ['PWD']
return os.path.normpath(os.path.join(curr_path, p))