In python is it possible to get or set a logical directory (as opposed to an absolute one).
For example if I have:
/real/path/to/dir
and I have
/linked/path/to/dir
linked to the same directory.
using os.getcwd and os.chdir will always use the absolute path
>>> import os >>> os.chdir('/linked/path/to/dir') >>> print os.getcwd() /real/path/to/dir
The only way I have found to get around this at all is to launch ‘pwd’ in another process and read the output. However, this only works until you call os.chdir for the first time.
The underlying operational system / shell reports real paths to python.
So, there really is no way around it, since
os.getcwd()is a wrapped call to C Librarygetcwd()function.There are some workarounds in the spirit of the one that you already know which is launching
pwd.Another one would involve using
os.environ['PWD']. If that environmnent variable is set you can make somegetcwdfunction that respects it.The solution below combines both: