> It's working (with Cyrus), but I have the feeling I'm doing it all wrong > because it seems so unwieldy.
I have a program, Polly <https://github.com/smontanaro/polly>, which I wrote to generate XKCD 936 passphrases. (I got the idea - and the name - from Chris Angelico. See the README.) It builds its dictionary from emails in my Gmail account which are tagged "polly" by a Gmail filter. I had put it away for a few years, at which time it was still using Python 2. When I came back to it, I wanted to update it to Python 3. As with so many 2-to-3 ports, the whole bytes/str problem was my stumbling block. Imaplib's API (as you've discovered) is not the most Pythonic. I didn't spend much time horsing around with it. Instead, I searched for higher-level packages, eventually landing on IMAPClient <https://pypi.org/project/IMAPClient/>. Once I made the switch, things came together pretty quickly, due in large part, I think, to its more sane API. YMMV, but you're more than welcome to steal code from Polly. Skip -- https://mail.python.org/mailman/listinfo/python-list