1

When executing a Python 3 script in a symbolic directory, I want to get the non-dereferenced path of the current directory. However, the default behavior of pathlib returns the derefenced path:

 $ mkdir test1
 $ ln -s test1 test2
 $ cd test2
 $ pwd
/home/myuser/test2
$ ipython3
Python 3.7.0 (default, Oct  9 2018, 10:31:47) 
Type 'copyright', 'credits' or 'license' for more information
IPython 7.1.1 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from pathlib import Path                                                                                                                  

In [2]: str(Path.cwd())                                                                                                                                
/home/myuser/test1

The behavior I want is to get "/home/myuser/test2" as that's where the 'script' (interpreter in this case) was executed from, preferably using pathlib.

1 Answer 1

1

Use os.getenv('PWD'):

Directories:

$ ls -l
total 1
drwxr-xr-x+ 1 cody agroup 0 Dec 11 15:23 dir1
lrwxrwxrwx  1 cody agroup 4 Dec 11 15:23 dir2 -> dir1

Result from dir2:

>>> str(Path.cwd())
'/home/cody/so/dir1'
>>> os.getenv('PWD')
'/home/cody/so/dir2'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.