MemoryError: Python 发送 zip 文件时内存不足
我有一个用来发送邮件的类。我正在尝试发送一个大约157MB的压缩文件作为附件,但我遇到了内存错误。当我发送其他小一点的压缩文件时,程序运行得很好。有没有人知道怎么解决这个问题,这样我就可以发送类似于导致问题的那些附件?
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"
这是我在尝试发送压缩文件时收到的错误信息
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
很遗憾,这一切都取决于你的代码能使用多少内存,以及你实际的内存限制和邮件服务器的限制。
你可以尝试把文件分成几块,然后在另一端再把它们合并起来,但这并不是一个理想的解决办法。压缩技术也可以帮忙,但效果非常有限。
还有一种比较复杂的解决方案是,如果你能访问运行邮件服务器的服务器,你可以暂时把附件写到服务器上,然后在邮件中留一个链接,让用户可以从服务器上下载那些超过一定大小的附件。