Where to locate existing standard encodings in python

rurpy at yahoo.com rurpy at yahoo.com
Tue Nov 11 23:50:06 CET 2008


On Nov 11, 11:19 am, Philip Semanchuk <phi... at semanchuk.com> wrote:
> On Nov 11, 2008, at 1:08 PM, News123 wrote:
>
> > Hi Philip,
>
> > Thanks for your answer:
> > The fact, that a module 'encodings' exists was new to me.
>
> We both learned something new today. =)
>
> > encodings.aliases.aliases has however one problem.
> > It helps to locate all encoding aliases, but it won't find entries for
> > which no aliases exist:
>
> Ooops, I hadn't thought about that.
>
> > What gives me a list of quite some encodings on my host is the shell
> > command
> > ls /usr/lib/python2.5/encodings  | sed -n 's/\.py$//p' | sort
> > (soma false hits, bit this is fine for me purposes)
>
> > I don't know if really all encodings are represented with a .py file
> > and
> > if all encodigns have to be in this directory, but it's a start.
>
> > Using shell commands is not that pythonic:
>
> > I could try to rewrite this in python by
> > 1.) determine from which directory encodings was imported and
> > then using the glob module to list all .py files located there.
>
> Yes, I'd thought about this but I agree with you that it seems
> unpythonic and fragile. Unfortunately I can't think of anything better
> at this point.
>
> Good luck
> Philip
...snip...

If it's of any help, in a post on 2007-07-22 by Peter Otten,
(though I can't get a url for it at the moment) he took the
same approach.  From a saved copy of that post:

import encodings
import os
import glob

def encodings_from_modulenames():
    ef = os.path.dirname(encodings.__file__)
    for fn in glob.glob(os.path.join(ef, "*.py")):
        fn = os.path.basename(fn)
        yield os.path.splitext(fn)[0]



More information about the Python-list mailing list