Python - 使用 OpenSharedFile 后如何关闭文件?
我尝试从 *.msg 文件中提取附件。我使用代码:
msg = outlook.OpenSharedItem(src_mail + name_mail)
经过一些操作(保存附件)后,我尝试重命名源文件
os.rename(source_dir + name_mail, source_dir + 'new.name')
,但出现 PermissionError:[WinError 32]。使用 OpenSharedItem 后如何关闭文件?文件不被其他进程使用(将重命名放在开头即可正常工作)。
完整代码:
import win32com.client
import os
import datetime
import sys
source_dir = r'//TP/dfs/G/glr_ARP/ARP/sap_HR/_maile/'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
for i in os.listdir(source_dir):
if i.endswith('.msg'):
name_mail = i
msg = outlook.OpenSharedItem(source_dir + name_mail)
for att in msg.Attachments:
att.SaveASFile(os.path.join(source_dir, str(att.FileName)))
os.rename(source_dir + name_mail, source_dir + 'a.a')
使用 OpenSharedItem 后如何关闭文件?
编辑: 不幸的是,建议的解决方案不起作用(使用或关闭)我尝试这样:
import win32com.client
import os
import datetime
import sys
source_dir = r'//TP/dfs/G/glr_ARP/ARP/sap_HR/_maile/test.msg'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
class OpenMsg():
def __init__(self, source):
self.source = source_dir
def __enter__(self):
self.msg = outlook.OpenSharedItem(self.source)
return self.msg
def __exit__(self, exc_type, exc_value, traceback):
self.msg.close(1)
with OpenMsg(source_dir) as msg:
print(msg)
os.rename(source_dir, source_dir + '.bak')
并且错误是相同的:PermissionError:[WinError 32]
I try to extract attachements from *.msg files. I using code:
msg = outlook.OpenSharedItem(src_mail + name_mail)
after some operations (save attachements) i try to reneame source file
os.rename(source_dir + name_mail, source_dir + 'new.name')
but I have PermissionError: [WinError 32]. How can I close file after use OpenSharedItem? File is not used by other process (place rename on the beginning works corectly).
Full code:
import win32com.client
import os
import datetime
import sys
source_dir = r'//TP/dfs/G/glr_ARP/ARP/sap_HR/_maile/'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
for i in os.listdir(source_dir):
if i.endswith('.msg'):
name_mail = i
msg = outlook.OpenSharedItem(source_dir + name_mail)
for att in msg.Attachments:
att.SaveASFile(os.path.join(source_dir, str(att.FileName)))
os.rename(source_dir + name_mail, source_dir + 'a.a')
How can I close file after use OpenSharedItem?
EDIT:
Unfortunately suggested solution do not work (with or close) i tryed like this:
import win32com.client
import os
import datetime
import sys
source_dir = r'//TP/dfs/G/glr_ARP/ARP/sap_HR/_maile/test.msg'
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
class OpenMsg():
def __init__(self, source):
self.source = source_dir
def __enter__(self):
self.msg = outlook.OpenSharedItem(self.source)
return self.msg
def __exit__(self, exc_type, exc_value, traceback):
self.msg.close(1)
with OpenMsg(source_dir) as msg:
print(msg)
os.rename(source_dir, source_dir + '.bak')
and error is the same: PermissionError: [WinError 32]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
os.rename()
之前使用:del msg
。这可能是最简单的方法。但是,我不确定是否还有其他下游影响。
我尝试过(但失败)的其他方法:
.Close()
方法,但错误消息是参数不是可选的。.Close(x)
,其中 x = 0,1,2,基于 Microsoft 文档 此处。del Outlook
,然后重新触发win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
但似乎有什么东西正在持有或锁定.msg
文件。Use:
del msg
beforeos.rename()
.This is the probably the simplest approach. However, I am not sure if there are other downstream implications.
Other approaches that I have tried (and failed):
.Close()
method but the error message is parameter is not optional..Close(x)
, where x = 0,1,2 based on Microsoft's documentation here.del outlook
, then retriggerwin32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
but something just seems to be holding or locking the.msg
file.