Python - replace backslash in string - rs.documentpath()

Trying to replace the backslash character in a string obtained with the rs.documentpath() method. The string replace function will not work since the backslash is an escape character. Any way to change my string into a raw string? Or any tips? I need to use the converted string with a os.chdir method.

Have you tried `\\`, which is one escaped backslash?

The following works for me, as @menno suggested

import rhinoscriptsyntax as rs

myString = rs.DocumentPath()
myString = myString.replace("\\"," ")
print myString

You might want to look at Python’s os.path module. It takes care of OS-specific path separator conventions. You should never have to do this the hard way.

1 Like

Thanks guys, replacing double backslashes worked. However I am still stuck passing my string to the os.chdir method. I though chdir doesn’t like backslashes, but even if I change those to forward slashes it still gives me a “windows error”. Is there any trick to make that work? Should I purge some characters from my path string or something?

Ok, all works now. Thanks guys.

1 Like