Is there a version of os.getcwd() that doesn't dereference symlinks? [duplicate]
Asked Answered
K

2

19

Possible Duplicate:
How to get/set logical directory path in python

I have a Python script that I run from a symlinked directory, and I call os.getcwd() in it, expecting to get the symlinked path I ran it from. Instead it gives me the "real" path, and in this case that's not helpful. I need it to actually give me the symlinked version.

Does Python have a command for that?

Kandi answered 9/10, 2009 at 9:32 Comment(0)
T
17

Workaround: os.getenv('PWD')

Tower answered 9/10, 2009 at 9:35 Comment(1)
Note that the PWD environment variable isn't updated by os.chdir(), so it can be inconsistent with os.getcwd() even when there are no symlinks at all.Haroun
A
13

In general this is not possible. os.getcwd() calls getcwd(3), and according to POSIX.1-2008 (IEEE Std 1003.1-2008):

The pathname shall contain no components that are dot or dot-dot, or are symbolic links.

os.getenv['PWD'] is shell-dependent and will not work for example with sh from FreeBSD.

Andromada answered 9/10, 2009 at 20:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.