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

Leo Kislov Leo.Kislov at gmail.com
Fri Nov 17 05:30:35 EST 2006


Martin v. Löwis 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?

How about returning two lists, first list contains unicode names, the
second list contains undecodable names:

files, troublesome = os.listdir(separate_errors=True)

and make separate_errors=True by default in python 3.0 ?

  -- Leo




More information about the Python-list mailing list