more on unescaping escapes

andrew cooke andrew at acooke.org
Mon Feb 23 19:10:31 EST 2009


do you know that a string with the letter "r" in front doesn't escape
slashes?  it's intended for regular expressions, but would simplify things
for you here too.

just do

  a=r'c:\\Program Files\test'

andrew


bvdp wrote:
>
> I'm getting hopelessly lost in a series of \\\\\\\ s :)
>
> Let's see if this makes sense:
>
>  >>> a='c:\\Program Files\\test'
>  >>> a.decode('string-escape')
> 'c:\\Program Files\test'
>
> In this case there are still 2 '\'s before the P; but only 1 before the
> 't'. Now, when it comes time to open the file windows accepts double
> '\'s in the filename. So, life is fine. But, the essential part here is
> that we're lucky we can use '\\' or '\' in a path. What if this wasn't
> true?
>
> The following shows a bit of difference:
>
>  >>> a='c:\Program Files\test'
>  >>> a
> 'c:\\Program Files\test'
>
> In this case the interpreter has changed the '\P' to '\\P'. And if one
> lists the string the '\t' really is a tab. No decode() at all in any of
> this.
>
> I guess the general rule would be to double up '\'s in filenames and (in
> my program's case) to use the \x20 for spaces.
>
> Thanks.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>





More information about the Python-list mailing list