Escaping slashes (double backslash plague)

Discussion in 'Python' started by Harry George, Jan 19, 2004.

  1. Harry George

    Harry George Guest

    Peter Hansen <> 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.


    --

    6-6M31 Knowledge Management
    Phone: (425) 342-5601
    Harry George, Jan 19, 2004
    #1
    1. Advertising

  2. 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/
    =?iso-8859-1?q?Aloysio=20Figueiredo?=, Jan 19, 2004
    #2
    1. Advertising

  3. Harry George

    anton muhin Guest

    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.
    anton muhin, Jan 19, 2004
    #3
  4. Harry George

    Peter Hansen Guest

    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
    Peter Hansen, Jan 19, 2004
    #4
  5. Harry George

    Peter Hansen Guest

    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...
    Peter Hansen, Jan 19, 2004
    #5
  6. Harry George

    Gerrit Holl Guest

    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/creaties/path/pep-xxxx.html
    Asperger's Syndrome - a personal approach:
    http://people.nl.linux.org/~gerrit/english/
    Gerrit Holl, Jan 19, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qazmlp
    Replies:
    5
    Views:
    686
    Michael Dunn
    Apr 7, 2004
  2. Vincent Texier
    Replies:
    3
    Views:
    2,265
    Vincent Texier
    Jul 1, 2004
  3. Luis P. Mendes

    backslash plague

    Luis P. Mendes, Oct 22, 2004, in forum: Python
    Replies:
    9
    Views:
    487
    Bengt Richter
    Oct 24, 2004
  4. tim
    Replies:
    1
    Views:
    318
  5. Dan Wilkin
    Replies:
    1
    Views:
    259
    robic0
    Jul 17, 2006
Loading...

Share This Page