Python replace / with \
Question: 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.


Answer: You missed something: it is _shown_ as `\\` by the Python interpreter, but
your result is correct: `'\\'`is just how Python represents the character `\`
in a normal string. That's strictly equivalent to `\` in a raw string, e.g.
`'some\\path` is same as `r'some\path'`.

And also: Python on windows knows very well how to use `/` in paths.

You can use the following trick though, if you want your dislpay to be OS-
dependant:

    
    
    In [0]: os.path.abspath('c:/some/path')
    Out[0]: 'c:\\some\\path'
    

