doubling slashes in a string

Joshua Muskovitz josh at open.com
Sat Oct 7 16:53:10 EDT 2000


Bzzt.  Thanks, but the point was to *double* the backslash.  The intended
result is not a legal pathname.  In fact, it is intended to go into yet
another interpreted language, and thus the backslashes needed to be escaped
(by doubling them).  The original string was a valid path.  Interestingly,
this is meant to work on a cross platform basis, and yet, only backslashes
need to be doubled, since they are the only special character needing to be
escaped.  On a *nix box, the doubling is not necessary.  Thus, the
split/join or replace is what is needed.

-- josh

"Kevin Cazabon" <kcazabon at home.com> wrote in message
news:OuJD5.419948$8u4.4894348 at news1.rdc1.bc.home.com...
> Got one better for you:  use the tool that was WRITTEN to do what you're
> trying to do.
>
> import os
>
> name = os.path.join("c:", "windows", "temp", "temp.file")
>
> print name
>
> >>> "c:\windows\temp\temp.file"
>
> name = os.path.split(name)
>
> print name
>
> >>> ["c:\windows\temp", "temp.file"]
>
> ###############
>
> This has the added advantage of being platform independant, joining names
> properly for all platforms.  You'll note the "single" slashes above, but
> they're recorded properly as doubles in the actual string:  when printed,
> they show up the way "intended".  if you do the above in command-line
mode,
> and simple type "name" instead of "print name", you'll see what I mean.
>
> Kevin.
>
>
> "Joshua Muskovitz" <josh at open.com> wrote in message
> news:39de8a48_3 at corp.newsfeeds.com...
> | Man, I just have to say, I *love* python.  I was struggling with the
> problem
> | of how to take a pathname and double all of the backslashes in it, so
that
> | (for example):
> |
> | c:\foo\bar\zingo.walla.walla --> c:\\foo\\bar\\zingo.walla.walla
> |
> | And I was trying for figure out how to walk the character list and build
a
> | new string, and then it dawned on me:
> |
> | newname = string.join(string.split(oldname,"\\"), "\\\\")
> |
> | It just keeps getting better and better.  Thanks for inventing it,
Guido!
> |
> | -- josh
> |
> |
> |
> |
> | -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> | http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> | -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----
>
>




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list