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 the file in this path:
/this/is/a/real/path/file.txt
and cd with a symbolic path name:
cd /this/is/a/link
now the pwd command will return the link name:
> pwd /this/is/a/link
and now I want to get the absolute file.txt path as:
/this/is/a/link/file.txt
but with python 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 got the real path instead of the symlink path.
How can I get a symbolic absolute path using python?
Update
OK, I read the pwd source code to get the answer.
It's pretty simple: just get the pwd environment variable.
This is my own abspath to satisfy my requirement:
def abspath(p): curr_path = os.environ['PWD'] return os.path.normpath(os.path.join(curr_path, p))
python symlink
ch.linghu
source share