backslash woes........

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Jul 10 12:35:21 CEST 2001


Martin Franklin <martin.franklin at westerngeco.com> wrote in
news:3B4AC7C2.DD53F6BB at westerngeco.com: 

> I am having trouble with windows path names (i'm a UNIX'ite)
> I want to replace the common prefix of a list of file names
> with a relative stub (./) I have this...
> 
>     def _removePrefix(self, fullfile):
>         print r'%s' %fullfile
Why are you using a raw string here? And why format it at all?
Either:
    	print "%s" % fullfile
or
    	print fullfile
will have the same effect.

>filename=re.sub(r'%s' %self.prefix, r'%s' %os.sep, r'%s' %fullfile)
Again the r prefix on each of the "%s" format strings does nothing, and in 
every case formatting a using a format of '%s' does nothing except to 
convert the value to a string if it wasn't already a string.
So this line is equivalent to:
   filename=re.sub(self.prefix, os.sep, fullfile)

If you want to use arbitrary strings in regular expressions, the best thing 
is to escape them using re.escape:
   filename=re.sub(re.escape(self.prefix), os.sep, fullfile)
but since your regular expression is actually a constant string, why use 
regular expressions at all? This is equivalent:
   filename=fullfile.replace(self.prefix, os.sep)

I think you maybe misunderstand what raw strings do. Raw strings simply 
prevent any backslash character that is present in the string from being 
interpreted as an escape sequence. They don't affect the processing or use 
of the string in any way. Since none of your literal strings contain 
backslashes there is no reason to use raw strings.
In regular expressions backslashes are special, but so are many other 
characters that could appear in filenames, even on Unix.

-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?



More information about the Python-list mailing list