[Python] Get pathlib Path with symlink

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    Let's say I open a python console inside /home/me/symlink/dir, which is actually a shortcut to /home/me/path/to/the/dir. In this console I execute the following code:

    from pathlib import Path
    path = Path.cwd()
    print(path) # "/home/me/path/to/the/dir"


    So cwd() actually resolves the symlinks and get the absolute path automatically and I am searching how to avoid this behavior. If the path with symlink is provided at the Path object declaration, it seems to work as I would like:

    from pathlib import Path
    path = Path("/home/me/symlink/dir")
    print(path) # "/home/me/symlink/dir"


    Is there a way to get the current working directory keeping the symlink other than providing the path at object instantiation ?

    Continue reading...

Compartilhe esta Página