MS Outlook CDO/MAPI 阻止 Python 文件输出?
这是我遇到的问题的一个例子。 我正在使用 Python Win32 扩展来访问 Outlook 邮箱并检索消息。
下面是一个应该将“hello world”写入文本文件的脚本。 我需要从 Outlook 邮箱中获取一些邮件,但我发现了一些奇怪的事情。 附加到邮箱一次后,我无法再将任何内容打印到文件中。 这是显示问题的精简版本:
#!/usr/bin/env python
from win32com.client import Dispatch
fh = open('foo.txt', 'w')
fh.write('hello ')
fh.close()
session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nprodreport');
session.Logoff()
fh = open('foo.txt', 'a')
fh.write('world')
fh.close()
如果我不附加到邮箱并注释掉以下行,它显然工作正常:
session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\ncorey');
session.Logoff()
为什么在我的脚本中间打开到邮箱的会话会阻止进一步的文件输出? 有任何想法吗? (其他操作不被阻止,只是这个文件i/o asfaik)
Here is an example of the problem I am running into. I am using the Python Win32 extensions to access an Outlook mailbox and retrieve messages.
Below is a script that should write "hello world" to a text file. I need to grab some messages from an Outlook mailbox and I noticed something weird. After I attach to the mailbox once, I can no longer print anything to a file. Here is a trimmed down version showing the problem:
#!/usr/bin/env python
from win32com.client import Dispatch
fh = open('foo.txt', 'w')
fh.write('hello ')
fh.close()
session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\nprodreport');
session.Logoff()
fh = open('foo.txt', 'a')
fh.write('world')
fh.close()
If I don't attach to the mailbox and comment out the following lines, it obviously works fine:
session = Dispatch('MAPI.session')
session.Logon('','',0,1,0,0,'exchange.foo.com\ncorey');
session.Logoff()
Why is opening a session to a mailbox in the middle of my script blocking further file output? any ideas? (other operations are not blocked, just this file i/o asfaik)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,目录更改是使用 CDO/MAPI 时的一个已知问题。 它在 MSDN 中的某处“记录”(例如 http://support.microsoft.com/kb/269170< /a>)。 您可以像这样在 Python 中轻松重现它:
Yes, the directory change is a known gotcha when using CDO/MAPI. It is "documented" somewhere in MSDN (eg http://support.microsoft.com/kb/269170). You can reproduce it easily in Python like this:
回答我自己的问题。 当您阅读电子邮件时,您的工作目录似乎已更改。 如果你将其设置回来,你的文件 i/o 工作正常。
正确的脚本如下所示:
answering my own question. it looks like your working directory gets changed when you read the email. If you set it back, your file i/o works fine.
the correct script would look like this: