# Add absolute name to pathlib.Path

**URL:** <https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882>\
**Category:** Ideas\
**Created:** [December 18, 2019, 11:46am UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882 "2019-12-18T11:46:19Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgirardet](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jgirardet/32/977_2.png) [@jgirardet](https://discuss.python.org/u/jgirardet)\
**Post date:** [December 18, 2019, 11:46am UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/1 "2019-12-18T11:46:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ericvsmith](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@ericvsmith](https://discuss.python.org/u/ericvsmith)\
**Post date:** [December 18, 2019, 12:35pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/2 "2019-12-18T12:35:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgirardet](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jgirardet/32/977_2.png) [@jgirardet](https://discuss.python.org/u/jgirardet)\
**Post date:** [December 18, 2019, 1:26pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/3 "2019-12-18T13:26:06Z")

</div>

Yes it is 🙂  
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.

---

<div class="post-metadata">

**Author:** ![uranusjr](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/uranusjr/32/103_2.png) [@uranusjr](https://discuss.python.org/u/uranusjr)\
**Post date:** [December 18, 2019, 1:35pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/4 "2019-12-18T13:35:36Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![asvetlov](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/asvetlov/32/55_2.png) [@asvetlov](https://discuss.python.org/u/asvetlov)\
**Post date:** [December 18, 2019, 1:57pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/5 "2019-12-18T13:57:43Z")

</div>

-1 for the idea

---

<div class="post-metadata">

**Author:** ![jgirardet](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jgirardet/32/977_2.png) [@jgirardet](https://discuss.python.org/u/jgirardet)\
**Post date:** [December 18, 2019, 2:06pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/6 "2019-12-18T14:06:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jgirardet](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jgirardet/32/977_2.png) [@jgirardet](https://discuss.python.org/u/jgirardet)\
**Post date:** [December 18, 2019, 2:17pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/7 "2019-12-18T14:17:49Z")

</div>

I read [Pathlib absolute() vs. resolve()](https://discuss.python.org/t/pathlib-absolute-vs-resolve/2573) 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.

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [December 18, 2019, 6:22pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/8 "2019-12-18T18:22:56Z")

</div>

> [@uranusjr](#):
>
> write the `str()` transform in that function (once).

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 😉).

> [@jgirardet](#):
>
> I read [Pathlib absolute() vs. resolve()](https://discuss.python.org/t/pathlib-absolute-vs-resolve/2573) and since the way to get an absolute path is not yet so clear

I’ve opened [Issue 39090: Document various options for getting the absolute path from pathlib.Path objects - Python tracker](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.

---

<div class="post-metadata">

**Author:** ![petersuter](https://avatars.discourse-cdn.com/v4/letter/p/13edae/32.png) [@petersuter](https://discuss.python.org/u/petersuter)\
**Post date:** [December 19, 2019, 8:44am UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/9 "2019-12-19T08:44:45Z")

</div>

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:

```python
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?

---

<div class="post-metadata">

**Author:** ![brettcannon](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/brettcannon/32/34895_2.png) [@brettcannon](https://discuss.python.org/u/brettcannon)\
**Post date:** [December 19, 2019, 5:33pm UTC](https://discuss.python.org/t/add-absolute-name-to-pathlib-path/2882/10 "2019-12-19T17:33:13Z")

</div>

> [@petersuter](#):
>
> Thanks for pointing out `os.fspath()` . Could you elaborate on its use?

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 [PEP 519 – Adding a file system path protocol | peps.python.org](https://www.python.org/dev/peps/pep-0519/) for the full rationale.
