path backslash escaping trouble

Gabriel Genellina gagsl-py2 at
Wed Jul 11 02:37:23 CEST 2007

En Mon, 09 Jul 2007 22:40:04 -0300, placid <Bulkan at> escribió:

> I have these files; which are Merge Request (ClearCase) files that are
> created by a Perl CGI script (being re-written in Python, as the HTML/
> JavaScript have been mixed with Perl, maintainability is zero)
> MergeType::::codefromlabel::::
> BLname::::BUILDMODS::::
> OldLname::::::::
> BaseVersion::::
> RequiredRelease::::
> Description::::::::
> FixRelation::::::::
> Dependencies::::::::
> LpAffected::::No::::
> CodeReview::::FirstName LastName::::
> Testing::::Compile/Build;Designer;Smoketests;::::
> OtherTesting::::::::
> Vobs::::ipsupport;::::
> Elements::::\ipsupport\ipbuild\Wizard\\main\buildmods\3::::
> i read this whole file into a string so i can search for the value of
> Elements which is
> \ipsupport\ipbuild\Wizard\\main\buildmods\3
> but this path is escaped
> \\ipsupport\\ipbuild\\Wizard\\\\main\\buildmods\\3
> so when i try to escape a string containing that same path using any
> of the os.path escaping methods doesnt
> result in the correct escaped path. It either appends "C:\\" in front
> of the string with all the backslashes escaped
> or it converts the three(3) at then end to "x03" and a match doesnt
> occur!

You may be confused about the actual string contents: "a\\b" contains  
exactly 3 characters, the second being a single backslash. The \ is the  
escape character; to include an actual \ inside a string, you have to  
double it. Another way is to use raw string literals (supressing escape  
processing): r"a\\b" contains four characters.
See section 3.1.2 in the Python tutorial or the Reference (more  

Gabriel Genellina

More information about the Python-list mailing list