Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow adding Path or str to Path #65997

Closed
gerritholl mannequin opened this issue Jun 17, 2014 · 4 comments
Closed

Allow adding Path or str to Path #65997

gerritholl mannequin opened this issue Jun 17, 2014 · 4 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@gerritholl
Copy link
Mannequin

gerritholl mannequin commented Jun 17, 2014

BPO 21798
Nosy @pitrou, @ericvsmith, @bitdancer, @gerritholl

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2014-06-18.17:39:25.968>
created_at = <Date 2014-06-17.21:10:40.696>
labels = ['type-feature', 'library']
title = 'Allow adding Path or str to Path'
updated_at = <Date 2014-06-18.17:39:25.967>
user = 'https://github.com/gerritholl'

bugs.python.org fields:

activity = <Date 2014-06-18.17:39:25.967>
actor = 'pitrou'
assignee = 'none'
closed = True
closed_date = <Date 2014-06-18.17:39:25.968>
closer = 'pitrou'
components = ['Library (Lib)']
creation = <Date 2014-06-17.21:10:40.696>
creator = 'Gerrit.Holl'
dependencies = []
files = []
hgrepos = []
issue_num = 21798
keywords = []
message_count = 4.0
messages = ['220893', '220896', '220943', '220944']
nosy_count = 4.0
nosy_names = ['pitrou', 'eric.smith', 'r.david.murray', 'Gerrit.Holl']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue21798'
versions = ['Python 3.4']

@gerritholl
Copy link
Mannequin Author

gerritholl mannequin commented Jun 17, 2014

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

@gerritholl gerritholl mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jun 17, 2014
@bitdancer
Copy link
Member

I would expect addition to return PosixPath('/tmp/some_base/_name.dat'), (ie: path.join).

@ericvsmith
Copy link
Member

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

@pitrou
Copy link
Member

pitrou commented Jun 18, 2014

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.

@pitrou pitrou closed this as completed Jun 18, 2014
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

3 participants