[Tutor] raw string notation.

D-Man dsh8290@rit.edu
Thu, 12 Jul 2001 16:05:54 -0400


On Thu, Jul 12, 2001 at 12:47:06PM -0700, Israel Evans wrote:
| 
| Hi there good people!
| 
| I'm performing a regular expression search and I'm running into some
| problems with the Raw String Notation.
| 
| when I type a string and I don't want any meta characters read as
| metacharacters, I thought that typing r in front of the quotes.  However in
| this particular circumstance, this doesn't seem to work
| 
| dir = r'c:\temp\test\'             #<--- this last single quote seems 
|                                          # to be getting escaped by the
| preceding slash
| SyntaxError: invalid token    #<--- hence this error.
| 
| Any ideas?

Yes -- in order to allow quotes to appear in raw strings, they are the
only escape sequence that is evaluated.  As a result, a raw string
can't end with a backslash.  There are a few solutions to this, in
your situation :

dir = r'c:\temp\test'

you don't really need the trailing backslash anyways.

dir = r'c:/temp/test'

Tim Peters pointed out that the windows API secretly allows forward
slashes instead of backslashes in a path.  This won't work with
command.com or cmd.exe, but will work for most other stuff (explorer
may not like it, though).

| Any Ideas?

Globing is good (from a Unix user's perspective, anyways <wink>).
Also take a look at the os.path module.  It may help you.

-D