views:

200

answers:

3

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?

+5  A: 

Workaround: os.getenv('PWD')

rq
Ah, thanks. That definitely works!
Gabriel Hurley
+3  A: 

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.

abbot