Bad raw string handling, string ends with backslash

Bengt Richter bokr at oz.net
Tue May 27 20:23:20 EDT 2003


On Tue, 27 May 2003 18:04:32 -0400, "Terry Reedy" <tjreedy at udel.edu> wrote:

>
>"Wolfgang" <wl at flexis.de> wrote in message
>news:bava04$6ap$1 at news.lf.net...
>> I have to do this:
>>
>> xyz = r"T:\bin\bla" + "\\"
>
>If this is for accessing directories or files on WindowsXXX,
>xyz = "T:/bin/bla/"
>may work (it does for me with Win9X for os.chdir() and file())
>
Also note that you don't need the '+', since the tokenizer will join
adjacent strings separated by whitespace (or no space). And the code
is a few nanoseconds faster too.

 >>> import dis
 >>> dis.dis( lambda: r"T:\bin\bla" + '\\')
           0 SET_LINENO               1
           3 LOAD_CONST               1 ('T:\\bin\\bla')
           6 LOAD_CONST               2 ('\\')
           9 BINARY_ADD
          10 RETURN_VALUE
 >>> dis.dis( lambda: r"T:\bin\bla"   '\\')
           0 SET_LINENO               1
           3 LOAD_CONST               1 ('T:\\bin\\bla\\')
           6 RETURN_VALUE
 >>> dis.dis( lambda: r"T:\bin\bla""\\")
           0 SET_LINENO               1
           3 LOAD_CONST               1 ('T:\\bin\\bla\\')
           6 RETURN_VALUE

But what would be wrong with having a version of raw string that
totally ignored the escape nature of backslashes? So you couldn't
quote certain combinations of quotes, you could always fall back
to current status. Say upper case R would work that way, then the OP
could write

    R"T:\bin\bla\"

what was the problem with that again?

Regards,
Bengt Richter




More information about the Python-list mailing list