Solved

Python: mailbox() and set_flag

Posted on 2009-05-12
5
300 Views
Last Modified: 2012-05-06
Hi experts,

I am writing Python code to process e-mails. This code only processes new e-mails. Whenever it finishes processing a new e-mail, it should update "Status". However, my code fails to do so. Anybody knows how to fix it?

Thanks so much.

mbox = mailbox.mbox('/var/spool/mail/' + user)

                for key, msg in mbox.iteritems():

                        flags = msg.get_flags()
 

                        if 'R' not in flags:

                                # process e-mails here

                                         try:

                                                msg.set_flag('R' + flags)

                                                mbox[key] = msg

                                                msg.flush()

                                        except:

                                                print "error in save 'R' info!"

Open in new window

0
Comment
Question by:davidw88
  • 3
  • 2
5 Comments
 
LVL 3

Accepted Solution

by:
ilalopoulos earned 125 total points
ID: 24373428
two changes at lines 8 and 10:
at line 8 the method name is wrong, the correct is set_flags
at line 10 flush() is a method of mbox and not msg
mbox = mailbox.mbox('/var/spool/mail/' + user)

    for k, msg in mbox.iteritems():

        flags = msg.get_flags()

        if 'R' not in flags:

            # process e-mails here

            try:

                msg.set_flags('R' + flags)

                mbox[k] = msg

                mbox.flush()

            except:

                print "error in save 'R' info!"

Open in new window

0
 

Author Comment

by:davidw88
ID: 24376231
Hi ilalopoulos,

Thanks so much for your reply. Following your suggestions I have modified my code, however this time I got an except "error in save 'R' info!". Further investigation led to the code
mbox.flush()
"Operation not permitted".

I also got this error once "I/O operation on closed file". It seems to me that I should open that mail file explicitely.

I googled online however I did not find a clue how to fix it. Do you have any ideas about this?

Thanks again.
0
 

Author Comment

by:davidw88
ID: 24376338
just fixed it...

thanks ilalopoulos.
0
 
LVL 3

Expert Comment

by:ilalopoulos
ID: 24376361
Check your permission rights to act on the files or execute your script with sudo i.e.:

sudo python your_script.py
0
 
LVL 3

Expert Comment

by:ilalopoulos
ID: 24376374
Glad to be of help, good luck with your project.

John
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python tuple index error 4 74
Python frameworks - maybe Django. 4 95
Can't install scipy on Python 2.7 in Win7 2 198
Python mysql Insert data error 3 82
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now