Escaping slashes (double backslash plague)

H

Harry George

Peter Hansen said:
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:


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.
 
?

=?iso-8859-1?q?Aloysio=20Figueiredo?=

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:

'\/'.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/
 
A

anton muhin

Aloysio said:
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:
why not replace: s.replace('\/', '/')?
"'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.
 
P

Peter Hansen

Aloysio said:
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:


'\/'.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:

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

-Peter
 
P

Peter Hansen

Aloysio said:
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...
 
G

Gerrit Holl

Peter said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top