os.lisdir, gets unicode, returns unicode... USUALLY?!?!?

gabor gabor at nekomancer.net
Thu Nov 16 19:26:08 EST 2006


Jean-Paul Calderone wrote:
> On Fri, 17 Nov 2006 00:31:06 +0100, "\"Martin v. Löwis\"" 
> <martin at v.loewis.de> wrote:
>> gabor schrieb:
>>>> All this code will typically work just fine with the current behavior,
>>>> so people typically don't see any problem.
>>>>
>>>
>>> i am sorry, but it will not work. actually this is exactly what i did,
>>> and it did not work. it dies in the os.path.join call, where file_name
>>> is converted into unicode. and python uses 'ascii' as the charset in
>>> such cases. but, because listdir already failed to decode the file_name
>>> with the filesystem-encoding, it usually also fails when tried with
>>> 'ascii'.
>>
>> Ah, right. So yes, it will typically fail immediately - just as you
>> wanted it to do, anyway; the advantage with this failure is that you
>> can also find out what specific file name is causing the problem
>> (whereas when listdir failed completely, you could not easily find
>> out the cause of the failure).
>>
>> How would you propose listdir should behave?
> 
> Umm, just a wild guess, but how about raising an exception which includes
> the name of the file which could not be decoded?
> 

i also recommend this approach.

also, raising an exception goes well with the principle of the least 
surprise imho.

gabor



More information about the Python-list mailing list