more on unescaping escapes

bvdp bob at mellowood.ca
Mon Feb 23 19:00:36 EST 2009


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.



More information about the Python-list mailing list