[New-bugs-announce] [issue21798] Allow adding Path or str to Path

Gerrit Holl report at bugs.python.org
Tue Jun 17 23:10:40 CEST 2014


New submission from Gerrit Holl:

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

----------
components: Library (Lib)
messages: 220893
nosy: Gerrit.Holl
priority: normal
severity: normal
status: open
title: Allow adding Path or str to Path
type: enhancement
versions: Python 3.4

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue21798>
_______________________________________


More information about the New-bugs-announce mailing list