1

In my python script I am trying to scan my inbox retrieve emails in reverse date-time order and by filtering on a subject. However, if my inbox has no emails with that subject, I get a NoneType error, object is not iterable" If there is an email with that subject, it all works fine. However when there is no email with that subject, I get this error. How to resolve it?

I have tried to use try catch blocks but the exception is not being caught

for item in account.inbox.all().filter(subject__contains='foo').order_by('-datetime_received')[:100]:

if there is no email with that subject, then there is a none type error.

if I check

if  (account.inbox.all().filter(subject__contains='foo').order_by('-datetime_received')[:100] is None):
print("none type error")

the print statement is not executed.

I would like to be able to skip the loop if there is no email at all in the mailbox

Nafeez Quraishi
  • 5,380
  • 2
  • 27
  • 34
  • slicing `[:100]` can't give `None` so `if item is None` can't work. It can give only empty list and better use universal `if not item:` – furas Jul 15 '19 at 14:14
  • you could also use `print()` to see what you have in variables `item` - maybe you get list with `[None, ...]` – furas Jul 15 '19 at 14:17
  • I cannot reproduce. Please post the entire stack trace of the failure. Also, which exchangelib version is this? – Erik Cederstrand Aug 05 '19 at 12:17

0 Answers0