A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2008-December/084158.html below:

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

[Python-Dev] Python-3.0, unicode, and os.environAnders 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4