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

断了今生、忘了曾经 提交于 2019-11-30 22:41:50

问题


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?


回答1:


Workaround: os.getenv('PWD')




回答2:


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.



来源:https://stackoverflow.com/questions/1542803/is-there-a-version-of-os-getcwd-that-doesnt-dereference-symlinks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!