【问题标题】:Python: change part (single directory name) of pathPython:更改路径的一部分(单个目录名称)
【发布时间】:2014-03-16 22:49:20
【问题描述】:

在路径中更改单个目录名称(仅第一次出现)的最佳方法是什么?

例子:

source_path = "/path/to/a/directory/or/file.txt"
target_path = "/path/to/different/directory/or/file.txt"

在这种情况下,指令将是:“将名称为'a'的第一个目录替换为名称为'不同'的目录”

我可以想出一些方法,首先将路径分成单个部分,然后找到第一个“a”,替换它并再次加入它。但我想知道是否有更优雅的方法来处理这个问题。也许是一个内置的python函数。

【问题讨论】:

    标签: python path directory


    【解决方案1】:

    有一个名为os.path.split 的函数可以将路径拆分为最后一部分并通向它,但这是您将得到的最接近的部分。因此,我们能做的最优雅的事情就是创建一个连续调用它的函数:

    import os, sys 
    def splitall(path): 
        allparts = [] 
        while 1: 
            parts = os.path.split(path) 
            if parts[0] == path: # sentinel for absolute paths 
                allparts.insert(0, parts[0]) 
                break 
            elif parts[1] == path: # sentinel for relative paths 
                allparts.insert(0, parts[1]) 
                break 
            else: 
                path = parts[0] 
                allparts.insert(0, parts[1]) 
                return allparts
    

    然后你可以像这样使用它,与os.path.join一起重新加入:

    >>> source_path = '/path/to/a/directory/or/file'
    >>> temp = splitall(source_path)
    >>> temp
    ['path', 'to', 'a', 'directory', 'or', 'file']
    >>> temp[2] = 'different'
    >>> target_path = os.path.join(*temp)
    >>> target_path
    'path/to/different/directory/or/file'
    

    【讨论】:

    • 谢谢!好吧,我想这确实是最简单的方法。
    【解决方案2】:

    如果我明白你想说的话,你想要这个:

    source_path = "/path/to/a/directory/or/file.txt"
    target_path = source_path.replace("/a/", "/different/", 1)
    print target_path
    

    【讨论】:

    • 好的,是的,没错。但我希望避免包含显式斜杠的“低级”字符串操作。
    • 嗯,你可以用正则表达式来做。
    【解决方案3】:

    使用https://docs.python.org/3/library/pathlib.html#module-pathlib:

    >>> from pathlib import PurePath
    >>> import os
    >>> path = PurePath("/path/to/a/directory/or/file.txt")
    >>> path.parts
    ('/', 'path', 'to', 'a', 'directory', 'or', 'file.txt')
    >>> a_idx = -1
    >>> for idx,part in enumerate(path.parts):
    ...   if part == 'a':
    ...     a_idx = idx
    ...     break
    ... 
    >>> a_idx
    3
    >>> pre_path = os.path.join(*path.parts[:a_idx])
    >>> post_path = os.path.join(*path.parts[a_idx+1:])
    >>> new_path = os.path.join(pre_path, 'different', post_path)
    >>> new_path
    '/path/to/different/directory/or/file.txt'
    

    【讨论】:

      【解决方案4】:

      如果您不知道目录的名称,只知道它的索引:

      from pathlib import Path
      
      source_path = Path("/path/to/a/directory/or/file.txt")
      unknown_name = source.parts[3] # position including root
      target_path = "/".join([part if part != unknown_name else "different" for part in source.parts])[1:]
      

      如果您知道目录的名称但不知道其索引,则几乎相同:

      from pathlib import Path
      
      source = Path("/path/to/a/directory/or/file.txt")
      src_parts = source.parts
      unknown_index = src_parts.index('a')
      target_path = "/".join([src_parts[part] if part != unknown_index else "different" for part in range(len(src_parts))])[1:]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-30
        • 1970-01-01
        • 2012-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多