我有一个 Python 脚本,可以获取收件箱文件夹中所有电子邮件的电子邮件 ID。但是,当 Outlook 达到数千封电子邮件时,会引发内存不足异常。
例外:
Printing emails...
Traceback (most recent call last):
File "print_emails.py", line 53, in main
print_emails()
File "print_emails.py", line 43, in print_emails
primary_emails, primary_email_ids = get_emails_and_ids(primary_source_folder)
File "print_emails.py", line 29, in get_emails_and_ids
property_accessor = item.PropertyAccessor
File "C:\Program Files\Python38\lib\site-packages\win32com\client\__init__.py", line 474, in __getattr__
return self._ApplyTypes_(*args)
File "C:\Program Files\Python38\lib\site-packages\win32com\client\__init__.py", line 467, in _ApplyTypes_
self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'Out of memory or system resources. Close some windows or programs and try again.', None, 0, -2147024882), None)
Press enter to exit...
我尝试了两种不同的方法:迭代一次(get_emails_and_ids)和两次(get_emails和get_email_ids)。
PropertyAccessor似乎和获得了几千次有关。如果我只是使用 来获取电子邮件get_emails,它在处理 38,000 封电子邮件时运行良好,但是当我开始使用数千次来获取 ID 时PropertyAccessor,内存就会耗尽。我必须释放旧的属性访问器吗?
安装:
pip install -U pypiwin32
代码:
#!/usr/bin/env python
from typing import Any, List, Tuple, Set
import traceback
import win32com.client
PidTagInternetMessageId = "http://schemas.microsoft.com/mapi/proptag/0x1035001F"
primary_account_email = "user@domain.tld"
primary_source_folder_name = "Inbox"
def get_emails(folder) -> List:
return [item for item in folder.Items if "_MailItem" in str(type(item))]
def get_email_ids(emails) -> Set[str]:
return {email_id for email in emails if len(email_id := email.PropertyAccessor.GetProperty(PidTagInternetMessageId)) > 0}
慕娘9325324
12345678_0001
相关分类