[Python-Dev] Python-3.0, unicode, and os.environ

Anders J. Munch ajm at flonidan.dk
Tue Dec 9 09:41:09 CET 2008


On Sun, Dec 7, 2008 at 3:53 PM, Terry Reedy <tjreedy at udel.edu> wrote:
>>> try:
>>>  files = os.listdir(somedir, errors = strict)
>>> except OSError as e:
>>>  log(<verbose error message that includes somedir and e>)
>>>  files = os.listdir(somedir)

Instead of a codecs error handler name, how about a callback for
converting bytes to str?

os.listdir(somedir, decoder=bytes.decode)
os.listdir(somedir, decoder=lambda b: b.decode(preferredencoding, errors='xmlcharrefreplace'))
os.listdir(somedir, decoder=repr)

ISTM that would be simpler and more flexible than going over the
codecs registry.  One caveat though is that there's no obvious way of
telling listdir to skip a name.  But if the default behaviour for
decoder=None is to skip with a warning, then the need to explicitly
ask for files to be skipped would be small.

Terry's example would then be:

>>> try:
>>>  files = os.listdir(somedir, decoder=bytes.decode)
>>> except UnicodeDecodeError as e:
>>>  log(<verbose error message that includes somedir and e>)
>>>  files = os.listdir(somedir)

- Anders


More information about the Python-Dev mailing list