I write some simple Python script and I want to replace all characters /
with \
in text variable. I have problem with character \
, because it is escape character. When I use replace()
method:
unix_path='/path/to/some/directory'
unix_path.replace('/','\\')
then it returns following string: \\path\\to\\some\\directory
. Of course, I can't use: unix_path.replace('/','\')
, because \
is escape character.
When I use regular expression:
import re
unix_path='/path/to/some/directory'
re.sub('/', r'\\', unix_path)
then it has same results: \\path\\to\\some\\directory
. I would like to get this result: \path\to\some\directory
.
Note: I aware of os.path
, but I did not find any feasible method in this module.