Add absolute name to pathlib.Path

Hi
this is just a simple idea,
The attribute name exists for Path class, which is a string.
The idea is only to add a aname which would be the equivalent of str(my_path.absolute()), that’s to say the absolute name. the aname word could be apath or full.

I propose this since I don’t find any other convenient way to do it

Jimmy

I don’t think we want convenience attributes for all possible Path methods. Is calling str(path.resolve()) really so bad?

Yes it is :slight_smile:
I really like pathlib because it’s powerful, easy and straightforward except it.
I find that when you use many absolute path, writing str(path.resolve()) is cumbersome.

By the way I already, use a custom subclass of Path with aname I and find it very useful.

I’m intrigued why you’d need many absolute paths in string form though. From my own experience this is not particularly useful except when you need to pass it to a foreign interface (e.g. as arguments to a subprocess). In which case it’s usually better to wrap that foreign call in a Python function that accepts Path, and write the str() transform in that function (once).

1 Like

-1 for the idea

2 Likes

You need It when working with config files for example, or storing image path in database.

I read Pathlib absolute() vs. resolve() and since the way to get an absolute path is not yet so clear, I understand My idea is not so simple it could seem.

And to be very specific, you want to use os.fspath() or one of os.fsencode()/os.fsdecode() depending on what you want to get out of the path object and not str() (otherwise you may end up setting a path as None accidentally :wink:).

I’ve opened https://bugs.python.org/issue39090 to track updating the docs as it’s as settled as it’s going to be, someone just needs to write it out so it’s a bit clearer for everyone how surprising complicated the situation is.

Thanks for pointing out os.fspath(). Could you elaborate on its use? Without already knowing what it does and why, I found the documentation not very illuminating. After thinking some time about it I assume you mean something like this:

import os
import pathlib
import typing

def command1(path_or_none: typing.Optional[pathlib.Path]) -> str:
    # Problem: This would be "cmd None" instead of "cmd ".
    return f"cmd {path_or_none}"

def command2(path_or_none: typing.Optional[pathlib.Path]) -> str:
    # Problem: This would be "cmd None" instead of "cmd ".
    return "cmd " + str(path_or_none)

def command3(path_or_none: typing.Optional[pathlib.Path]) -> str:
    # No problem. Verbose.
    if path_or_none is None 
        return "cmd"
    return f"cmd {path_or_none}" 

def command4(path_or_none: typing.Optional[pathlib.Path]) -> str:
    # No problem. Requires arcane knowledge.
    return "cmd " + os.fspath(path_or_none)

os.system(command1(None))
os.system(command2(None))
os.system(command3(None))
os.system(command4(None))

Or is there something more to it?

This is getting a bit off-topic, but basically you won’t want str(None) to work as that’s not a path. os.fspath() makes sure you only work with path-like objects, str, or bytes. See https://www.python.org/dev/peps/pep-0519/ for the full rationale.