Python IMAP4 select mailbox name contain space cha

2019-06-03 16:08发布

问题:

I am using Python imaplib.IMAP4 to connect to Google email. Every thing is okay except when I use IMAP4.select() method using a mailbox name that contains a space character. Let's say my mailbox name is "Gama Sutra".

When I execute imap.select('[Gmail]/Gama Sutra') it gives me this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 682, in select
typ, dat = self._simple_command(name, mailbox)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 1133, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imaplib.py", line 964, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: SELECT command error: BAD [b'Could not parse command']

How do I address that?

回答1:

It should do this automatically, but simply surround your folder name in double quotes:

imap.select('"[Gmail]/Gama Sutra"')