title: Unable to retrieve gmail messages from any folder other than inbox (Python3 issue)
body: <p><strong>Update:</strong> my code works under python 2.6.5 but not python 3 (I'm using 3.4.1).</p>
  
  <p>I'm unable to search for messages in the "All Mail" or "Sent Mail" folders - I get an exception:</p>
  
  <pre><code>imaplib.error: SELECT command error: BAD [b'Could not parse command']
  </code></pre>
  
  <p>my code:</p>
  
  <pre><code>import imaplib
  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
  m.login("myemail@gmail.com","mypassword")
  m.select("[Gmail]/All Mail")
  </code></pre>
  
  <p>using <code>m.select("[Gmail]/Sent Mail")</code> doesn't work either.</p>
  
  <p>But reading from the inbox works: </p>
  
  <pre><code>import imaplib
  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
  m.login("myemail@gmail.com","mypassword")
  m.select("inbox")
  ...
  </code></pre>
  
  <p>I used the mail.list() command to verify the folder names are correct:</p>
  
  <pre><code>b'(\\HasNoChildren) "/" "INBOX"', 
  b'(\\Noselect \\HasChildren) "/" "[Gmail]"',
  b'(\\HasNoChildren \\All) "/" "[Gmail]/All Mail"', 
  b'(\\HasNoChildren \\Drafts) "/" "[Gmail]/Drafts"', 
  b'(\\HasNoChildren \\Important) "/" "[Gmail]/Important"', 
  b'(\\HasNoChildren \\Sent) "/" "[Gmail]/Sent Mail"', 
  b'(\\HasNoChildren \\Junk) "/" "[Gmail]/Spam"', 
  b'(\\HasNoChildren \\Flagged) "/" "[Gmail]/Starred"', 
  b'(\\HasNoChildren \\Trash) "/" "[Gmail]/Trash"'
  </code></pre>
  
  <p>I'm following the solutions from these questions, but they don't work for me:<br>
  <a href="https://stackoverflow.com/questions/10570889/imaplib-what-is-the-correct-folder-name-for-archive-all-mail-in-gmail/25185126#comment39217313_25185126">imaplib - What is the correct folder name for Archive/All Mail in Gmail?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3267234/i-cannot-search-sent-emails-in-gmail-with-python/25185265#25185265">I cannot search sent emails in Gmail with Python</a></p>
  
  <p>Here is a complete sample program that <strong>doesn't work on Python 3</strong>:</p>
  
  <pre><code>import imaplib
  import email
  
  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
  m.login("myemail@gmail.com","mypassword")
  m.select("[Gmail]/All Mail")
  
  result, data = m.uid('search', None, "ALL") # search all email and return uids
  if result == 'OK':
      for num in data[0].split():
          result, data = m.uid('fetch', num, '(RFC822)')
          if result == 'OK':
              email_message = email.message_from_bytes(data[0][1])    # raw email text including headers
              print('From:' + email_message['From'])
  
  m.close()
  m.logout()
  </code></pre>
  
  <p>The following exception is thrown:</p>
  
  <pre><code>Traceback (most recent call last):
  File "./eport3.py", line 9, in &lt;module&gt;
  m.select("[Gmail]/All Mail")
  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 682, in select
  typ, dat = self._simple_command(name, mailbox)
  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 1134, in _simple_command
  return self._command_complete(name, self._command(name, *args))
  File "/RVM/lib/python3/lib/python3.4/imaplib.py", line 965, 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']
  </code></pre>
  
  <p>Here's the corresponding Python 2 version <strong>that works</strong>:</p>
  
  <pre><code>import imaplib
  import email
  
  m = imaplib.IMAP4_SSL("imap.gmail.com", 993)
  m.login("myemail@gmail.com","mypassword")
  m.select("[Gmail]/All Mail")
  
  result, data = m.uid('search', None, "ALL") # search all email and return uids
  if result == 'OK':
      for num in data[0].split():
          result, data = m.uid('fetch', num, '(RFC822)')
          if result == 'OK':
              email_message = email.message_from_string(data[0][1])    # raw email text including headers
              print 'From:' + email_message['From']
  
  m.close()
  m.logout()
  </code></pre>