doubling slashes in a string
Alex Martelli
aleaxit at yahoo.com
Mon Oct 9 09:56:06 EDT 2000
More information about the Python-list mailing list
Mon Oct 9 09:56:06 EDT 2000
- Previous message (by thread): doubling slashes in a string
- Next message (by thread): doubling slashes in a string
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
"Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message news:slrn8u3i4t.fgl.scarblac at pino.selwerd.nl... > Alex Martelli <aleaxit at yahoo.com> wrote in comp.lang.python: > > Whoops! You're right. I had never noticed this lexical peculiarity, > > and now I wonder about the rationale for it -- since backslashes play > > no special role within a rawstring, why the peculiarly specific prohibition > > about having an odd number of them _at the end_...? > > They still have a special role - they escape quotes. r"\"" still needs > to be equal to '"'. If r"\"" really needs to be equal to '"', then we're in serious trouble, at least in Python 2.0: Python 2.0b2 (#6, Sep 26 2000, 14:59:21) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.6 -- press F1 for help >>> r"\"" '\\"' >>> i.e., r"\"" rather seems to be equal to \\\". Still, you're right, that quote IS playing a role -- it inserts itself in the string AND also inserts the following quoting-character (while, without the \, said following quoting-character would otherwise terminate the rawstring). Whether it "should" do either or both things is moot, it definitely IS doing them both. > As a consequence, a single \ at the end of a raw > string always escapes the quote, so the string isn't closed. Yep, pretty clear now. > > Oh well, then I guess my above-quoted suggestions will have to be reworded: > > > > newname = oldname.split('\\').join(r'\\') > > newname = oldname.replace('\\', r'\\') > > Note that if he's doing things with pathnames on Windows, it might > be a lot easier to use / instead of \ to start with (that works). I think the original poster explained that he needs to prepare a string to be passed to another program (dunno if by system, popen, or what else), so he *cannot* assume that / will work just the same as \ (on the commandline of many DOS and Windows programs, the equivalence does not hold). I guess the doubling is what he needs to protect the backslashes from one level of stripping (whoever may be doing that...). Alex
- Previous message (by thread): doubling slashes in a string
- Next message (by thread): doubling slashes in a string
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list