Re: problems with opening files due to file's path
- From: Carsten Haese <carsten.haese@xxxxxxxxx>
- Date: Wed, 11 Jun 2008 10:14:58 -0400
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
.
- Follow-Ups:
- References:
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Mike Driscoll
- Re: problems with opening files due to file's path
- From: Grant Edwards
- Re: problems with opening files due to file's path
- From: Carsten Haese
- Re: problems with opening files due to file's path
- From: Alexnb
- Re: problems with opening files due to file's path
- From: Lie
- Re: problems with opening files due to file's path
- Prev by Date: Re: Alternative to Decimal type
- Next by Date: My fight with classes :)
- Previous by thread: Re: problems with opening files due to file's path
- Next by thread: Re: problems with opening files due to file's path
- Index(es):
Relevant Pages
|