Problem getting a file pathname with tkFileDialog

Eric Brunel eric_brunel at despammed.com
Thu Nov 9 03:17:53 EST 2006


On Wed, 08 Nov 2006 21:59:38 +0100, Tim Daneliuk <tundra at tundraware.com>  
wrote:

> Sefyroth wrote:
>> Thanks,
>>  but I get this error when I try this.
>>  UnicodeEncodeError: 'ascii' codec can't encode character u'\xe8' in
>> position 12: ordinal not in range(128)
>>   I had encountered it with the askdirectory method as well. Is there an
>> easy way to bypass this?
>>  Thanks again
>
> I believe you are running into a directory or file name that has
> non-ascii characters in it.  Python as shipped is set up to
> deal with ascii as its native encoding format.  You can change
> this by editing the "site.py" file - look in the Lib directory
> in your python installation.  Look for this code:
>
> -------------------------------
> def setencoding():
>      """Set the string encoding used by the Unicode implementation.  The
>      default is 'ascii', but if you're willing to experiment, you can
>      change this."""
>      encoding = "ascii" # Default value set by _PyUnicode_Init()
>      if 0:
>          # Enable to support locale aware default string encodings.
>          import locale
>          loc = locale.getdefaultlocale()
>          if loc[1]:
>              encoding = loc[1]
> -------------------------------
>
>
>
> Change the "if 0:" to "if 1:" and see if that doesn't fix the problem.

This is usually a bad idea, especially if the script must be distributed  
to other users in any way: since they probably never did this "trick", the  
code will fail when they use it.

I know it's a pain, but you *have* to deal with encodings yourself, and  
not let the system guess what you might want.

In the OP's case, the problem just lies in the 'print' statement, that  
tries to encode the file name in ASCII before printing it. So just doing:
print repr(file.name)
would solve the problem.

HTH
-- 
python -c "print ''.join([chr(154 - ord(c)) for c in  
'U(17zX(%,5.zmz5(17l8(%,5.Z*(93-965$l7+-'])"



More information about the Python-list mailing list