【问题标题】:Python pathlib - is this a correct way to join paths in a platform independent way?Python pathlib - 这是以独立于平台的方式加入路径的正确方法吗?
【发布时间】:2020-05-09 07:26:14
【问题描述】:

我已经开始将我想在 posix 和 windows 上运行的代码从 os.path 转换为 pathlib.Path 模块。具体来说,我换了-

import os
os.path.join('grandparent_dir', 'parent_dir', 'file_name')

与

from pathlib import Path
Path('grandparent_dir', 'parent_dir', 'file_name')

它似乎有效,但我在documentation 中找不到这种实例化类的方法。这是好的做法吗?

【问题讨论】:

    标签: python-3.x path


    【解决方案1】:

    在documentation on Pure Paths,我刚刚找到了一个实例化的例子-

    PurePath('foo', 'some/path', 'bar')
    

    由于pathlib.Path 是subclass of PurePath,它也必须是有效的pathlib.Path

    【讨论】:

      猜你喜欢
      • 2021-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-05
      • 2012-09-18
      相关资源
      最近更新 更多