472,958 Members | 2,233 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 6841
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Randell D. | last post by:
Folks, I'm using Apache/1.3.28 (SuSE 7.1, kernal 2.4) with PHP/4.3.2. I have the following code to help cleanse form data. function cleanData($sourceData, &$cleanData) { foreach($myData as...
5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
0
by: Reply Via Newsgroup Thanks | last post by:
Folks, This questions is directed towards PHP/MySQL folk and relates to escaping hooks, apostraphe's and other characters that can create a security hole when writing to databases/files. I've...
14
by: Jon Maz | last post by:
Hi, I have been getting hopelessly confused with escaping escape characters in JScript! All I want to do is write a simple funtion: function DoubleUpBackSlash(inputString) { ??????? }
2
by: StevePBurgess | last post by:
Ok. I have checked the magic quotes is on. I have a form on my web page. The input from this is sent (by POST) to the server. A PHP script processes it. If there any errors the input is urlencoded...
7
by: Matthew Warren | last post by:
Hi, I would expect this to work, rawstring=r'some things\new things\some other things\' But it fails as the last backslash escapes the single quote. ...although writing this I think I...
3
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only...
3
by: placid | last post by:
Hi All, I have these files; which are Merge Request (ClearCase) files that are created by a Perl CGI script (being re-written in Python, as the HTML/ JavaScript have been mixed with Perl,...
7
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.