I am using python3 in my ubuntu machine. I have a string variable which consist a path with backslash and i need to convert it to forward slash string. So i tried
import pathlib
s = '\dir\wnotherdir\joodir\more'
x = repr(s)
p = pathlib.PureWindowsPath(x)
print(p.as_posix())
this will print correctly as
/dir/wnotherdir/joodir/more
but for different other string path, it acts weirdly. Ex, for the string,
'\dir\aotherdir\oodir\more'
it correctly replacing backslashes but the value is wrong because of the character 'a' in original string
/dir/x07otherdir/oodir/more
What is the reason for this behavior?
\ais widely interpreted as the ascii BEL character (07), so it looks like repr(), is performing this translation. A shallow search suggests you should be usingx = x.replace("\\", "/")to correct the path separator.