Python - 使用 OpenSharedFile 后如何关闭文件?

发布于 2025-01-16 06:57:25 字数 1683 浏览 3 评论 0原文

我尝试从 *.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')

Error

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泪之魂 2025-01-23 06:57:25

os.rename()之前使用:del msg

这可能是最简单的方法。但是,我不确定是否还有其他下游影响。

我尝试过(但失败)的其他方法:

  1. .Close() 方法,但错误消息是参数不是可选的。
  2. .Close(x),其中 x = 0,1,2,基于 Microsoft 文档 此处
  3. del Outlook,然后重新触发 win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") 但似乎有什么东西正在持有或锁定 .msg 文件。

Use: del msg before os.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):

  1. .Close() method but the error message is parameter is not optional.
  2. .Close(x), where x = 0,1,2 based on Microsoft's documentation here.
  3. del outlook, then retrigger win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI") but something just seems to be holding or locking the .msg file.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文