How to refer to "\" sign in python string
Question: I have problem with refering to special symbol in string:

I have: `path='C:\dir\dir1\dir2\filename.doc'`

and I want `filename`.

When I try: `filename=path[path.rfind("\"):-4]`

then interpreter says it's an error line right from "\" since is treated as a
comment.


Answer: You can use `"\\"`, technically it would be better to use `os.path.sep` if you
insist on using backslashes. But better yet, use `/` in your paths, it works
fine on Windows

Python has [builtin functions to manipulate
paths](http://docs.python.org/library/os.path.html). Note that you need to
double the backslashes if you still prefer them to forwardslashes

    
    
    >>> import os
    >>> path='C:\\dir\\dir1\\dir2\\filename.doc'
    >>> os.path.splitext(os.path.basename(path))
    ('filename', '.doc')
    

and using forwardslashes

    
    
    >>> path='C:/dir/dir1/dir2/filename.doc'
    >>> os.path.splitext(os.path.basename(path))
    ('filename', '.doc')
    

