“加入Path”之间的区别和“/&quot”在Pathlib中

发布于 2025-02-11 23:45:36 字数 333 浏览 1 评论 0原文

JOINPath/code> pathlib模块中的操作员之间是否存在区别?该文档永远不会比较两种方法。本质上,这两个情况有所不同吗?

例子:


from pathlib import Path

foo = Path("some_path")
foo_bar_operator = foo / "bar"
foo_bar_joinpath = foo.joinpath("bar")

foo_bar_operator == foo_bar_joinpath
# Returns: True

Is there a difference between joinpath and the / operator in the pathlib module? The documentation doesn't ever compare the two methods. Essentially are there any cases where these two are different?

Example:


from pathlib import Path

foo = Path("some_path")
foo_bar_operator = foo / "bar"
foo_bar_joinpath = foo.joinpath("bar")

foo_bar_operator == foo_bar_joinpath
# Returns: True

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-02-18 23:45:36

没有区别。

    def joinpath(self, *args):
        """Combine this path with one or several arguments, and return a
        new path representing either a subpath (if all arguments are relative
        paths) or a totally different path (if one of the arguments is
        anchored).
        """
        return self._make_child(args)

    def __truediv__(self, key):
        try:
            return self._make_child((key,))
        except TypeError:
            return NotImplemented

”将多个参数传递到joinpath eg a.joinpath(b,c,d)/ < / code>的等效词是a / b / c / d < / code>。

There is no difference. The source code confirms this:

    def joinpath(self, *args):
        """Combine this path with one or several arguments, and return a
        new path representing either a subpath (if all arguments are relative
        paths) or a totally different path (if one of the arguments is
        anchored).
        """
        return self._make_child(args)

    def __truediv__(self, key):
        try:
            return self._make_child((key,))
        except TypeError:
            return NotImplemented

Note you can pass multiple arguments to joinpath e.g. a.joinpath(b, c, d). The equivalent for / would be a / b / c / d.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文