Home > Mobile >  Python redirect the path by detection of ../ from anothers path
Python redirect the path by detection of ../ from anothers path

Time:01-20

I have a path in variable A

A=r'\\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Proton\Coach_Color_Dress.xml

I have anothers path B

B="..\..\Type\Car\Proton.xml"

By using python I would like to print entire path for path B which redrive from Path A Expected output for C is:

C=r'\\omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Type\Car\Proton.xml'

Anyone have ideas?

CodePudding user response:

You could use the powerful pathlib module:

from pathlib import Path

a = A.replace('\\', '/')
b = B.replace('\\', '/')

c = Path(a) / Path(b)
print(c.resolve())

gives /omega3t.cr.in.com/shop/recipe/fad/prod/CPL/Wite/Type/Car/Proton.xml

You should check if that's really what you want. It's strange to use .. on a file path, usually that is used on a directory path.

If you really need backslashes you can still replace them:

str(c.resolve()).replace('/', '\\')

gives \omega3t.cr.in.com\shop\recipe\fad\prod\CPL\Wite\Type\Car\Proton.xml

  •  Tags:  
  • Related