Re: problems with opening files due to file's path



Lie wrote:
In most GUI toolkits (including Tkinter) and raw_input() function,
when you input a string (using the textbox, a.k.a Entry widget) it
would automatically be escaped for you, so when you input 'path\path
\file.txt', the GUI toolkit would convert it into 'path\\path\
\file.txt'.

That's incorrect. If you enter text into a text box or in raw_input(), *no* conversion of backslashes is happening. A backslash entered in raw_input is just a backslash. A backslash entered in a textbox is just a backslash. A backslash read from a file is just a backslash.

A "conversion" happens when you print the repr() of a string that was obtained from raw_input or from a text box, because repr() tries to show the string literal that would result in the contents, and in a string literal, a backslash is not (always) a backslash, so repr() escapes the backslashes:

py> text = raw_input("Enter some text: ")
Enter some text: This is a backslash: \
py> print text
This is a backslash: \
py> print repr(text)
'This is a backslash: \\'

As you can see, I entered a single backslash, and the string ends up containing a single backslash. Only when I ask Python for the repr() of the string does the backslash get doubled up.

--
Carsten Haese
http://informixdb.sourceforge.net
.



Relevant Pages

  • string replace for back slash
    ... here with escape sequence. ... You are running into the difference between the 'repr' of a string (which ... In the repr the backslash needs to be escaped ...
    (comp.lang.python)
  • Re: gfortran diagnostics and so on
    ... Well, in f0003, backslash is part of the standard Fortran character set. ... Because the backslash is part of the standard Fortran character set, the default behavior should be the printable character, **NOT** some kind of magic introductory character that transforms the interpretation of following character. ... The one I like best is to use one of the popular extensions to designate a particular literal string according to the C language. ...
    (comp.lang.fortran)
  • Re: Double backslashes \ in strings
    ... and the single backslash isn't ... The simplest way to specify a string is to enclose it in single quotes. ... echo ''; ...
    (comp.lang.php)
  • Re: Convert to /
    ... |> Why is it so hard to convert backslashes to forward slashes in java? ... | character immediately after the colon would be the tab ... | get a backslash character into a string literal in Java ...
    (comp.lang.java.help)
  • Re: Raw String Question
    ... backslashes are left in the string. ... backslash as the last character of a string without doubling it". ... regular expressions without doubling all the backslashes. ...
    (comp.lang.python)