Question : Python email exception

Hi experts,

I try to use the attached code to retrieve e-mails, however I got

david@xadapp3:~/test$ sudo python EmailContent.py
Traceback (most recent call last):
  File "EmailContent.py", line 52, in
    email.mailstats('geek')
  File "EmailContent.py", line 26, in mailstats
    str  = self.msgfactory(fp)
  File "EmailContent.py", line 14, in msgfactory
    except email.errors.MessageParseError:
AttributeError: EmailContent instance has no attribute 'errors'


I have imported "email", "mailbox", etc.

Anybody knows why I got an error?

Thanks so much.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
def msgfactory(self, fp):
        try:
            return email.message_from_file(fp)
        except email.errors.MessageParseError:
        # Don't return None since that will
        # stop the mailbox iterator
            return ''
 
    def mailstats(self, user):
        if os.path.isfile('/var/spool/mail/'+user):
            fp=open('/var/spool/mail/'+user)
        else:
            print 'I cannot open mail for user',user
            sys.exit(1)
 
        str  = self.msgfactory(fp)
        mbox = mailbox.UnixMailbox(fp, str)
 
      ..................
Open in New Window Select All

Answer : Python email exception

If so, you should avoid rebinding names of imported modules. Otherwise, how can you refer to the module named 'email', after you assign an object to the name 'email' (ie. email = 'something else but not the module').
Random Solutions  
 
programming4us programming4us