'Using Python mailbox to delete old msgs, lock doesn't work

I have a very busy server with plenty of processes scheduled with cron and each sends an email so the mailbox gets a lot of messages. I want to delete old messages from previous months and of course, I could use mutt and do it by hand, but it is more practical to do it automatically. This is my current code using Python's mailbox:

import mailbox
import email.errors
import datetime
from dateutil.parser import parse

mbox = mailbox.mbox('/var/mail/lanotadm')
to_remove = []
hoy = datetime.datetime.today().strftime("%Y%m")

for key, msg in mbox.iteritems():                                               
    dt = parse(msg['Date'])
    idt = dt.strftime("%Y%m")
    if idt < hoy:
        to_remove.append(key)

mbox.lock()
try:
    for key in to_remove:
        mbox.remove(key)
finally:
    mbox.lock()
    mbox.flush()
    mbox.close()

When I try to run it I got this error which indicates obviously that lock is not working:

~$ bin/archiborra.py
Traceback (most recent call last):
  File "/home/lanotadm/bin/archiborra.py", line 30, in <module>
    mbox.flush()
  File "/usr/lib/python3.9/mailbox.py", line 672, in flush
    raise ExternalClashError('Size of mailbox file changed '
mailbox.ExternalClashError: Size of mailbox file changed (expected 1207545369, found 1207546669)

I know lock is not working because the problem happens when I try to flush, to update the mailbox.

The problem happens in both Debian/exim4 and Centos7/Postfix.

EDIT. I tried adding .copy() as suggested by JayantSeth. No change in Centos but in Debian apparently it does and go to the next error about permission.

~$ bin/archiborra.py
Traceback (most recent call last):
  File "/home/lanotadm/bin/archiborra.py", line 30, in <module>
    mbox.flush()
  File "/usr/lib/python3.9/mailbox.py", line 676, in flush
    new_file = _create_temporary(self._path)
  File "/usr/lib/python3.9/mailbox.py", line 2122, in _create_temporary
    return _create_carefully('%s.%s.%s.%s' % (path, int(time.time()),
  File "/usr/lib/python3.9/mailbox.py", line 2114, in _create_carefully
    fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_RDWR, 0o666)
PermissionError: [Errno 13] Permission denied: '/var/mail/lanotadm.1652389699.tsom02.667230'

which is weird because /var/mail/lanotadm belongs to lanotadm.

~$ ls -l /var/mail/lanotadm
-rw-rw---- 1 lanotadm mail 1214771087 May 12 21:11 /var/mail/lanotadm

What can I do? I will appreciate any hint.



Solution 1:[1]

try making a copy of to_remove: Replace for key in to_remove: with for key in to_remove.copy():

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JayantSeth