473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 6879
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.