problems with opening files due to file's path

Carsten Haese carsten.haese at gmail.com
Wed Jun 11 16:14:58 CEST 2008


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



More information about the Python-list mailing list