python中msg是什么意思_无法使用python分析for循环中的.msg文件

我是Python编程的新手。我需要自动复制大约450个“msg”文件,这些文件位于Windows中一个目录的多个子目录中,根据每个“msg”文件中的内容复制到其他目录。我使用了以下代码:from shutil import copy2

import win32com.client

# Function which returns dictionary with absolute filepaths, file names

def list_files(dir):

r = {}

for root, dirs, files in os.walk(dir):

for name in files:

r[os.path.join(root, name)]=name

return r

allFiles = list_files(sourceDir)

# Parsing all emails

for filename in allFiles.items():

if filename[1].endswith(".msg"):

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

try:

email = outlook.OpenSharedItem(filename[0])

emailContent = email.Subject + "\n" + email.Body

print(emailContent)

except FileNotFoundError as e:

print("File Not Found Error: " + str(e))

finally:

del email, outlook

接下来,我将根据内容复制文件。但这些电子邮件的许多文件路径都超过260个字符。所以,我得到了以下错误:

^{pr2}$

如果我重命名文件名,使绝对路径长度减少到260个字符以下,我就不会遇到这个错误。但是我需要完整的文件名。我还尝试用以下代码绕过MAX_PATH限制,但在本例中无效:email = outlook.OpenSharedItem("\\\\?\\"+filename[0])

我尝试了另一种方法,为每次迭代更改当前的工作目录。代码如下:for filename in allFiles.items():

if filename[1].endswith(".msg"):

os.chdir(filename[0].replace(filename[1],""))

outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")

try:

email = outlook.OpenSharedItem(filename[1])

emailContent = email.Subject + "\n" + email.Body

print(emailContent)

...

但是,我面临以下错误:com_error Traceback (most recent call last)

in ()

5 try:

----> 6 email = outlook.OpenSharedItem(filename[1])

7 emailContent = email.Subject + "\n" + email.Body

~\Anaconda3\lib\site-packages\win32com\client\dynamic.py in OpenSharedItem(self, Path)

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open '2013 Self Cert.msg'. It's possible the file is already open, or you don't have permission to open it.\n\nTo check your permissions, right-click the file folder, then click Properties.", None, 0, -2147287038), None)

我不知道这个错误背后的原因。有人能帮帮我吗?在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值