This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow adding Path or str to Path
Type: enhancement Stage:
Components: Library (Lib) Versions: Python 3.4
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: Gerrit.Holl, eric.smith, pitrou, r.david.murray
Priority: normal Keywords:

Created on 2014-06-17 21:10 by Gerrit.Holl, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (4)
msg220893 - (view) Author: Gerrit Holl (Gerrit.Holl) * Date: 2014-06-17 21:10
It would be great if we could "add" either str or Path to Path objects.  Currently, we can join paths only by adding a directory, or by replacing the suffix.  But sometimes we want to build up a path more directly.  With strings we can do that simply by concatenating strings.  It would be great if we could do the same with Path objects, like this:

In [2]: b = pathlib.Path("/tmp/some_base")

In [3]: b + "_name.dat"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-939467ea0aa5> in <module>()
----> 1 b + "_name.dat"

TypeError: unsupported operand type(s) for +: 'PosixPath' and 'str'

In [4]: b + pathlib.Path("_name.dat")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-460bf8ac7710> in <module>()
----> 1 b + pathlib.Path("_name.dat")

TypeError: unsupported operand type(s) for +: 'PosixPath' and 'PosixPath'

In [11]: b.with_name(b.name + "_name.dat")  # desired effect
Out[11]: PosixPath('/tmp/some_base_name.dat')
msg220896 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-06-17 21:24
I would expect addition to return PosixPath('/tmp/some_base/_name.dat'), (ie: path.join).
msg220943 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2014-06-18 17:36
I agree with David. And, it already works, but uses '/', not '+':

>>> b / "_name.dat"
PosixPath('/tmp/some_base/_name.dat')

-1 to using + to be the equivalent of:

>>> pathlib.Path(str(b) + "_name.dat")
PosixPath('/tmp/some_base_name.dat')
msg220944 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2014-06-18 17:39
Indeed, not trying to pretend to act like a string was one of the design points of the pathlib module, and the "/" operator already performs logical path joining. I'm therefore closing this issue as rejected.
History
Date User Action Args
2022-04-11 14:58:05adminsetgithub: 65997
2014-06-18 17:39:25pitrousetstatus: open -> closed
resolution: rejected
messages: + msg220944
2014-06-18 17:36:40eric.smithsetnosy: + eric.smith
messages: + msg220943
2014-06-17 21:24:15r.david.murraysetnosy: + r.david.murray, pitrou
messages: + msg220896
2014-06-17 21:10:40Gerrit.Hollcreate