[Tkinter-discuss] listdir
Michael Lange
klappnase at web.de
Fri Feb 16 04:24:59 EST 2018
Hi,
On Fri, 16 Feb 2018 08:49:01 +0000
Vasilis Vlachoudis <Vasilis.Vlachoudis at cern.ch> wrote:
> Thank you Michael,
>
> my locale is UTF-8 when I get the error
>
> $ locale
> LANG=en_US.UTF-8
> LANGUAGE=en_US.UTF-8
> LC_CTYPE="en_US.UTF-8"
> ...
> LC_ALL=en_US.UTF-8
ok. Don't know if it is possible that there is something in the configuration
that stops python from properly detecting the system encoding? Some ten
years ago (while using Python 2.3 :) I wrote myself a function that tries
to detect the system encoding (see below); since it is so old some of the things
that function does may be obsolete now, but I still use it today in
a number of my projects and it still works. If you like you can run this
snippet as a test script and see what it says.
Of course it is also well possible that my guess was wrong and the problem
lies somewhere else.
Best regards
Michael
###################################################
import codecs
import locale
def _find_codec(encoding):
# return True if the requested codec is available, else return False
try:
codecs.lookup(encoding)
return 1
except (TypeError, LookupError):
return 0
def _sysencoding():
# try to guess the system default encoding
# this is mainly stolen from IDLE's IOBinding.py
# thanks for guidance to Martin v. Loewis
locale.setlocale(locale.LC_CTYPE, "")# don't mess the numeric types here, we might replace the decimal point with a comma and break Tk!
try:
enc = locale.getpreferredencoding()
if enc and _find_codec(enc):
return enc
except AttributeError:
# our python is too old, try something else
pass
try:
enc = locale.nl_langinfo(locale.CODESET)
if enc and _find_codec(enc):
return enc
except AttributeError:
pass
# the last try
try:
enc = locale.getdefaultlocale()[1]
if enc and _find_codec(enc):
return enc
except ValueError:
pass
# aargh, nothing good found, fall back to ascii and hope for the best
print 'warning: unable to find usable encoding, using ascii.'
return 'ascii'
print(_sysencoding().lower())
###################################################
.-.. .. ...- . .-.. --- -. --. .- -. -.. .--. .-. --- ... .--. . .-.
Vulcans believe peace should not depend on force.
-- Amanda, "Journey to Babel", stardate 3842.3
More information about the Tkinter-discuss
mailing list