Python 通过从其他路径检测到 ../ 来重定向路径
Python redirect the path by detection of ../ from anothers path
我在变量 A 中有一个路径
A=r'\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Proton\Coach_Color_Dress.xml
我有另一个路径B
B="..\..\Type\Car\Proton.xml"
通过使用 python 我想打印从路径 A 重新驱动的路径 B 的整个路径
C 的预期输出是:
C=r'\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Type\Car\Proton.xml'
有人有想法吗?
您可以使用功能强大的 pathlib
模块:
from pathlib import Path
a = A.replace('\', '/')
b = B.replace('\', '/')
c = Path(a) / Path(b)
print(c.resolve())
给出 /omega3t.cr.in.com/shop/recipe/fad/prod/CPL/Wite/Type/Car/Proton.xml
您应该检查一下这是否真的是您想要的。在文件路径上使用..
很奇怪,通常是在目录路径上使用
如果您确实需要反斜杠,您仍然可以替换它们:
str(c.resolve()).replace('/', '\')
给出 \omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Type\Car\Proton.xml
我在变量 A 中有一个路径
A=r'\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Proton\Coach_Color_Dress.xml
我有另一个路径B
B="..\..\Type\Car\Proton.xml"
通过使用 python 我想打印从路径 A 重新驱动的路径 B 的整个路径 C 的预期输出是:
C=r'\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Type\Car\Proton.xml'
有人有想法吗?
您可以使用功能强大的 pathlib
模块:
from pathlib import Path
a = A.replace('\', '/')
b = B.replace('\', '/')
c = Path(a) / Path(b)
print(c.resolve())
给出 /omega3t.cr.in.com/shop/recipe/fad/prod/CPL/Wite/Type/Car/Proton.xml
您应该检查一下这是否真的是您想要的。在文件路径上使用..
很奇怪,通常是在目录路径上使用
如果您确实需要反斜杠,您仍然可以替换它们:
str(c.resolve()).replace('/', '\')
给出 \omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Type\Car\Proton.xml