This doesn’t work:
import re
re.sub('\\', '/', "C:\\Users\\Judge")
Error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
re.sub('\\', '/', "C:\\Users")
File "C:\Python27\lib\re.py", line 151, in sub
return _compile(pattern, flags).sub(repl, string, count)
File "C:\Python27\lib\re.py", line 244, in _compile
raise error, v # invalid expression
error: bogus escape (end of line)
Try escaping two backslashes instead of one:
\\\\re.sub('\\\\', '/', "C:\\Users\\Judge")You are just passing the RE engine one backslash, which confuses it. So, not only do you have to escape the backslashes for Python to be happy, you need to escape it again for RE. Since you are escaping two backslashes, you need four total.
If you aren’t using any regular expression features, perhaps you’d be better off with string’s simpler replace method:
'C:\\Users\\Judge'.replace('\\', '/')