I'm writing a cross platform file explorer in python. I am trying to convert any backslashes in a path into forward slashes in order to deal with all paths in one format.
I've tried not only using string.replace(str, '\\', '/'), but also creating a method manually to search through the string and replace the instances, and both do not work properly, as a path name such as:
\dir\anotherdir\foodir\more
changes to:
/dir/anotherdir\x0oodir/more
I am assuming that this has something to do with how Python represents escape characters or something of the sort. How do I prevent this happening?
r'\dir\anotherdir\foodir\more'.replace('\\', '/')works just fine. - Glenn Maynard\dir\anotherdir\foodir\moreas a string yourself, and\fis special. If you want Python not to interpret special characters (characters prefixed by backslashes) you should use “raw” strings, e.g:r'\dir\anotherdir\foodir\more'- tzotos.path.abspathwill convert them to unified format. - Lei Yang