开发者

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

开发者 https://www.devze.com 2022-12-08 06:45 出处:网络
This question already has answers her开发者_JS百科e: Closed 10 years ago. Possible Duplicate: How to get/set logical directory path in python
This question already has answers her开发者_JS百科e: Closed 10 years ago.

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?


Workaround: os.getenv('PWD')


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消