backslash woes........

Martin Franklin martin.franklin at westerngeco.com
Tue Jul 10 12:47:15 CEST 2001


Duncan Booth wrote:
> 
> 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.


Forgot to remove this before I sent the code in sorry... I was
jst testing around to _see_ if I could _see_ what was happening.....


> 
> >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)


Tried re.escape() to escape my backslashes...  The jury is still out on this
IOW I need to go back and test it again.

> 
> 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.


You are right I don't understand...  My strings do include backslashes (they
are windows filenames from os.path.walk())  I Have indeed changed to using 
string.replace() - having read the HOW TO on www.python.org.... and it seems 
to work (without using raw strings....) This all seems very confusing! 


Thanks for you help
Martin

> 
> --
> 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