Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Escaping slashes (double backslash plague)

Reply
Thread Tools

Escaping slashes (double backslash plague)

 
 
Harry George
Guest
Posts: n/a
 
      01-19-2004
Peter Hansen <(E-Mail Removed)> writes:

> Aloysio Figueiredo wrote:
> >
> > I need to replace every ocurrence of '/' in s by '\/'
> > in order to create a file named s. My first attempt
> > was:
> >
> > s = '\/'.join(s.split('/'))
> >
> > but it doesn't work:
> >
> > >>> s = 'a/b'
> > >>> s = '\/'.join(s.split('/'))
> > >>> s

> > 'a\\/b'
> > >>> repr(s)

> > "'a\\\\/b'"
> > >>>

> >
> > '\/'.join() escapes the backslashes and I don't know why.

>
> It does not, although *you* are not escaping the backslash
> yourself, and that is dangerous. Get in the habit of always
> escaping your own backslashes, so that if you ever happen
> to use a backslash followed by one of the characters which _is_
> a valid escape sequence, you won't get confused.
>
> '\/' == '\\/'
>
> but
>
> '\t' != '\\t'
>
> The first example shows two ways of writing a string with the blackslash
> character followed by a forward slash. The second example shows a TAB
> character on the left, but a backslash plus the letter 't', on the right.
>
> As for your apparent automatic escaping of backslashes: when you show
> results in an interactive session by just typing the expression, such as
> when you do ">>> s" you will see the repr() of the value, not the actual
> content. Use print instead and you'll see the difference:
>
> >>> print s

>
> This is all covered pretty well, I think, by the Python tutorials and
> such. Have you gone through those?
>
> -Peter


Did someone already mention os.path? Since this is about filenames,
that is the best cross-platform colution.


--
http://www.velocityreviews.com/forums/(E-Mail Removed)
6-6M31 Knowledge Management
Phone: (425) 342-5601
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Aloysio=20Figueiredo?=
Guest
Posts: n/a
 
      01-19-2004
I need to replace every ocurrence of '/' in s by '\/'
in order to create a file named s. My first attempt
was:

s = '\/'.join(s.split('/'))

but it doesn't work:

>>> s = 'a/b'
>>> s = '\/'.join(s.split('/'))
>>> s

'a\\/b'
>>> repr(s)

"'a\\\\/b'"
>>>


'\/'.join() escapes the backslashes and I don't know
why.

Any help?

Aloysio Figueiredo

__________________________________________________ ____________________

Yahoo! GeoCities: a maneira mais fácil de criar seu web site grátis!
http://br.geocities.yahoo.com/

 
Reply With Quote
 
 
 
 
anton muhin
Guest
Posts: n/a
 
      01-19-2004
Aloysio Figueiredo wrote:
> I need to replace every ocurrence of '/' in s by '\/'
> in order to create a file named s. My first attempt
> was:
>
> s = '\/'.join(s.split('/'))
>
> but it doesn't work:
>
>
>>>>s = 'a/b'
>>>>s = '\/'.join(s.split('/'))

why not replace: s.replace('\/', '/')?

>>>>s

>
> 'a\\/b'
>
>>>>repr(s)

>
> "'a\\\\/b'"

Try print s, and you'll see what you want.
>
>
> '\/'.join() escapes the backslashes and I don't know
> why.
>
> Any help?
>
> Aloysio Figueiredo
>

regards,
anton.
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-19-2004
Aloysio Figueiredo wrote:
>
> I need to replace every ocurrence of '/' in s by '\/'
> in order to create a file named s. My first attempt
> was:
>
> s = '\/'.join(s.split('/'))
>
> but it doesn't work:
>
> >>> s = 'a/b'
> >>> s = '\/'.join(s.split('/'))
> >>> s

> 'a\\/b'
> >>> repr(s)

> "'a\\\\/b'"
> >>>

>
> '\/'.join() escapes the backslashes and I don't know why.


It does not, although *you* are not escaping the backslash
yourself, and that is dangerous. Get in the habit of always
escaping your own backslashes, so that if you ever happen
to use a backslash followed by one of the characters which _is_
a valid escape sequence, you won't get confused.

'\/' == '\\/'

but

'\t' != '\\t'

The first example shows two ways of writing a string with the blackslash
character followed by a forward slash. The second example shows a TAB
character on the left, but a backslash plus the letter 't', on the right.

As for your apparent automatic escaping of backslashes: when you show
results in an interactive session by just typing the expression, such as
when you do ">>> s" you will see the repr() of the value, not the actual
content. Use print instead and you'll see the difference:

>>> print s


This is all covered pretty well, I think, by the Python tutorials and
such. Have you gone through those?

-Peter
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      01-19-2004
Aloysio Figueiredo wrote:
>
> I need to replace every ocurrence of '/' in s by '\/'
> in order to create a file named s.


Harry inspired me to reread your question, but now I think you might
be very confused about something.

Are you trying to create a file whose name contains a forward
slash? And you think that by "escaping" the slash with a backslash,
you can do this?

If so, give up: it's not possible. File names cannot contain a
forward slash, at least on most any operating system which uses slashes
as path separators. (*)

If this isn't what you're trying to do, please explain more thoroughly
what your goal is, because it seems very unnecessary to be putting
\/ into a string for any reason (whether as a path or not) ...

-Peter

(*) Examples to the contrary, while perhaps interesting, notwithstanding...
 
Reply With Quote
 
Gerrit Holl
Guest
Posts: n/a
 
      01-19-2004
Peter Hansen wrote:
> If so, give up: it's not possible. File names cannot contain a
> forward slash, at least on most any operating system which uses slashes
> as path separators. (*)


> (*) Examples to the contrary, while perhaps interesting, notwithstanding...


There is (was?) a bug in I think NFS or SMBFS which allowed the creation
of a file with a '/' in it. I heard of someone which had a very tough
time in removing it again

open(''.join([chr(i) for i in range(1, 256) if chr(i) != '/']), 'w')-ly y'rs - Gerrit

--
254. If he take the seed-corn for himself, and do not use the yoke of
oxen, he shall compensate him for the amount of the seed-corn.
-- 1780 BC, Hammurabi, Code of Law
--
PrePEP: Builtin path type
http://people.nl.linux.org/~gerrit/c.../pep-xxxx.html
Asperger's Syndrome - a personal approach:
http://people.nl.linux.org/~gerrit/english/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
path backslash escaping trouble placid Python 3 07-12-2007 05:31 AM
RegEx: odd number of slashes? and too many slashes? Dan Wilkin Perl Misc 1 07-17-2006 03:47 AM
how to replace double backslash with one backslash in string... Vincent Texier Python 3 07-01-2004 01:03 PM
String replacement( 2 slashes-> 4 slashes ) qazmlp Java 5 04-07-2004 10:26 PM
Using slashes as querystring Nils N ASP .Net 2 02-08-2004 09:37 PM



Advertisments