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
Comments
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 |
I would expect addition to return PosixPath('/tmp/some_base/_name.dat'), (ie: path.join). |
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') |
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. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: