gpt4 book ai didi

python - 尝试使用 Python 3.7.2 和 IMAPClient 批量删除电子邮件 - imaplib.IMAP4.error : UID command error: BAD [b'Command line too large']

转载 作者:行者123 更新时间:2023-12-01 08:26:44 25 4
gpt4 key购买 nike

我对编程非常陌生,所以我在这里寻求一些帮助。我有一个包含超过 20 万封电子邮件的电子邮件文件夹。我试图将它们全部删除,因为它们拖慢了我的速度。我使用 IMAPClient 在 Python 3.7.2 中编写了一个脚本来尝试批量删除它们,但我不断收到错误。

我尝试删除文件夹中的所有电子邮件,并且尝试删除一天的电子邮件。平均一天有大约 2000 封电子邮件。同时使用“ALL”和“ON DD-MMM-YYYY”

import imapclient
imapObj = imapclient.IMAPClient('imap.server.com', ssl=True)
imapObj.login('email', 'password')

import pprint
pprint.pprint(imapObj.list_folders())

[((b'\\NoInferiors',), b'/', 'INBOX'),
((b'\\HasNoChildren',), b'/', 'Folder I want to delete'),
--all folders--

imapObj.select_folder('Folder I want to delete', readonly=False)
{b'PERMANENTFLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft',
b'\\Deleted', b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk',
b'$MailFlagBit1', b'$MailFlagBit0', b'$MailFlagBit2', b'\\*'),
b'FLAGS': (b'\\Answered', b'\\Flagged', b'\\Draft', b'\\Deleted',
b'\\Seen', b'$Forwarded', b'$NotJunk', b'NotJunk', b'$MailFlagBit1',
b'$MailFlagBit0', b'$MailFlagBit2'), b'EXISTS': 3792507, b'RECENT':
0, b'UNSEEN': [b'3792504'], b'UIDVALIDITY': 1398179813, b'UIDNEXT':
7577201, b'READ-WRITE': True}

UIDs = imapObj.search('ALL')
imapObj.delete_messages(UIDs)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
imapObj.delete_messages(UIDs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1171, in delete_messages
return self.add_flags(messages, DELETED, silent=silent)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1081, in add_flags
return self._store(b'+FLAGS', messages, flags, b'FLAGS',
silent=silent)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1567, in _store
uid=True)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-
packages/imapclient/imapclient.py", line 1535, in
_command_and_check
typ, data = self._imap.uid(command, *args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 879, in uid
typ, dat = self._simple_command(name, command, *args)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 1196, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/imaplib
.py", line 1027, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.IMAP4.error: UID command error: BAD [b'Command line too large']

我希望删除电子邮件,但该命令只是出现上述错误。

最佳答案

UId 列表太长,无法发出一个请求,我们需要拆分每次删除。

UIDs = imapObj.search('ALL')
for UID in UIDs
imapObj.delete_messages([UID])
print( 'Deleted--->'+str(UID))

关于python - 尝试使用 Python 3.7.2 和 IMAPClient 批量删除电子邮件 - imaplib.IMAP4.error : UID command error: BAD [b'Command line too large'],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54186291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com