471,310 Members | 1,073 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Escaping slashes (double backslash plague)

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/

Jul 18 '05 #1
5 6715
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.
Jul 18 '05 #2
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
Jul 18 '05 #3
Peter Hansen <pe***@engcorp.com> 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.
--
ha************@boeing.com
6-6M31 Knowledge Management
Phone: (425) 342-5601
Jul 18 '05 #4
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...
Jul 18 '05 #5
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/

Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Randell D. | last post: by
5 posts views Thread by sinister | last post: by
reply views Thread by Reply Via Newsgroup Thanks | last post: by
2 posts views Thread by StevePBurgess | last post: by
7 posts views Thread by Matthew Warren | last post: by
3 posts views Thread by Taras_96 | last post: by
3 posts views Thread by placid | last post: by
7 posts views Thread by wannymahoots | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.