更改路径中的单个目录名称(仅第一次出现)的最佳方法是什么?
示例:
source_path = "/path/to/a/directory/or/file.txt"
target_path = "/path/to/different/directory/or/file.txt"
在这种情况下,指令将是:“将名称为 'a' 的第一个目录替换为名称为 ' different' 的目录”
我可以想到首先将路径分成各个部分的方法,然后找到第一个“a”,替换它并再次连接它。但我想知道是否有更优雅的方法来处理这个问题。也许是内置的 python 函数。
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'
如果我明白你想说的话,你想要这个:
source_path = "/path/to/a/directory/or/file.txt"
target_path = source_path.replace("/a/", "/different/", 1)
print target_path
使用 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'
如果您不知道目录名称,只知道其索引:
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:]
只是为了完成 anon582847382 答案。如果您不希望通过自定义 splitall 函数:
path = os.path.normpath(path)
path.split(os.sep)