[Tutor] Raw string

Steve Willoughby steve at alchemy.com
Mon Apr 19 01:06:46 CEST 2010

On Sun, Apr 18, 2010 at 06:48:47PM -0400, bob gailer wrote:
> On 4/18/2010 5:49 PM, Neven Gor??i?? wrote:
> >When I get file path from DirDialog, I get in a (path) variable.
> >Sometimes that string (path) contains special escape sequences, such 
> >as \x, \r and so on.

Since this is a tutorial list, it might be good to point out a few
distinctions at work here.  First of all, strings do not ever contain
escape sequences.  They simply contain characters.

So once a string is returned from DirDialog, it just contains characters.
For example, 'C:\Python25\Programs\rating', or in other words: C, colon,
backslash, P, y, t, and so forth.  I would imagine that handing that to
the open() function should work on a Windows platform just fine.  Handing
it to another function which may try to interpret the characters specially
might not, but as Bob Gailer pointed out,

> "It doesn't work" is too vague. Please show us a code snippet that 
> includes obtaining the file path, attempt to open and what happens. Do 
> you get a traceback (error message)? If so, please post it with the code.

However, you mentioned escape sequences and the 'r' marker.  These are not
things which are stored IN strings, but are part of the Python language as
tools to enable us to craete strings which  may have specific special 
characters in them.  So IN MY PROGRAM SOURCE CODE (ONLY) I could put a
bit of Python code like this:

          var = 'hello, world\n'

which contains the escape code \n which *in source code* tells Python
to create the string with a newline character where the \n was in the source.
There is not escape code in the string (which is referenced now by the name
var).  That string just contains the characters: h, e, l, l, o, comma, space,
w, o, r, l, d, newline.

If I used the "r" marker, that tells Python to avoid doing the above, so
the source code

          rawvar = r'hello, world\n'

would create the string: h, e, l, l, o, comma, space, w, o, r, l, d,
backslash, n.  But those, at this point (once that line of code was 
executed) are just characters in the string.

> >             'C:\Python25\Programs\rating'

If that were a line in a program as a string literal value, like
          filepath = 'C:\Python25\Programs\rating'

Then you'd have a problem, indeed.  That would need to use the 'r'
marker, or properly escape the backslashes:
          filepath = r'C:\Python25\Programs\rating'
          filepath = 'C:\\Python25\\Programs\\rating'

However, if the DirDialog returned a string value like:

meaning the string C, colon, backslash, P, y, ..., then that's probably
exactly what it's supposed to do, and we'd need to see more code and/or
actual output to figure out what the real source of trouble might be.
> >I know that raw string marker 'r' in front of string leaves char '\' 
> >as character and not as start of escape sequences,
> >but I can not apply it to a variable name which contains file path.

I hope the explanation above helps you to understand why such appliction
is not only impossible, but wouldn't really even make sense to do.

> >Is there a function with same effect as raw string marker, as my 
> >problem must be solved differently?

If you mean to take a string which contained the same characters as
Python recognizes as escape sequences, like "backslash n" and turns them
into things like "newline", then yes, actually, there are several ways
Python can do that, but I'm almost certain that's not really your problem
here so I don't want to point you down the wrong path.

Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.

More information about the Tutor mailing list