MemoryError:在Python中发送zip文件时内存不足

时间:2014-06-06 17:40:30

标签: python python-2.7

我有班级发送电子邮件。我正在尝试发送一个大约157 MB的zip文件附件,但我收到内存错误。当我发送其他较小的zip文件时,该程序工作正常。有谁知道如何处理这个问题所以我可以发送类似于导致问题的附件吗?

class Sender: 
#constructor
def __init__(self,filename):
    self.filename = filename    

#functions
def message(self):
    fromaddr = "myaddress@gm.com"
    toaddr = ["address"]        
    msg = MIMEMultipart()
    msg['From'] = fromaddr
    msg['To'] = ",".join(toaddr)
    msg['Subject'] = "New Base Year"

    # This is the binary part(The Attachment):
    part = MIMEApplication(open('t:\\base_year_06_06_2014.zip',"rb").read())
    part.add_header('Content-Disposition', 'attachment', filename='srping2013.zip')

    msg.attach(part)        
    body = "Hello,"
    msg.attach(MIMEText(body, 'plain'))
    server = smtplib.SMTP('c11', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login("myaddress@gm.com", "password")
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)
    server.quit()

    return "Message Sent"

这是我在尝试发送zip文件时遇到的错误

Traceback (most recent call last):
File "C:\Python27\ArcGIS10.1\Lib\site- packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
exec codeObject in __main__.__dict__
File "T:\Python scripts\scripts\test\zipper.py", line 58, in <module>
success = send_email.message()
File "T:\Python scripts\scripts\test\zipper.py", line 36, in message
text = msg.as_string()
File "C:\Python27\ArcGIS10.1\lib\email\message.py", line 137, in as_string
g.flatten(self, unixfrom=unixfrom)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 108, in _write
self._dispatch(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 134, in _dispatch
meth(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 203, in _handle_multipart
g.flatten(part, unixfrom=False)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 83, in flatten
self._write(msg)
File "C:\Python27\ArcGIS10.1\lib\email\generator.py", line 118, in _write
self._fp.write(sfp.getvalue())
MemoryError: out of memory

1 个答案:

答案 0 :(得分:0)

不幸的是,这完全基于您的代码分配了多少内存以及您在物理上以及在电子邮件服务器上的内存限制。

您可以尝试将文件拆分成碎片,然后将它们放在另一端,但这不是一个理想的解决方案。压缩技术也有帮助,但只能以非常小的方式。

另一个非常复杂的解决方案是,如果您有权访问运行邮件服务器的服务器,您可以暂时将附件写入服务器,并在电子邮件中保留一个链接,允许用户下载更大的附件。超出服务器本身的一定大小。