无法使用python分析for循环中的.msg文件

无法使用python分析for循环中的.msg文件,python,outlook,Python,Outlook,我是Python编程的新手。我需要根据每个“msg”文件中的内容,自动将位于Windows中一个目录的多个子目录中的大约450个“msg”文件复制到其他目录。我使用了以下代码: from shutil import copy2 import win32com.client # Function which returns dictionary with absolute filepaths, file names def list_files(dir): r = {} for

我是Python编程的新手。我需要根据每个“msg”文件中的内容,自动将位于Windows中一个目录的多个子目录中的大约450个“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个字符。因此,我得到以下错误:

com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The URL was bad/not found:\r\n  "C:\\Users\\HPHP\\Documents\\...\\Data\\...\\IHI ...\\......". Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)
com_error                                 Traceback (most recent call last)
<ipython-input-64-c7dea54d9247> in <module>()
      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)
如果我重命名文件名,使绝对路径长度减少到260个字符以下,我就不会遇到这个错误。然而,我需要的文件名是完整的。我还尝试使用以下代码绕过MAX_路径限制,但在这种情况下不起作用:

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: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The URL was bad/not found:\r\n  "C:\\Users\\HPHP\\Documents\\...\\Data\\...\\IHI ...\\......". Cannot find this file. Verify the path and file name are correct.', None, 0, -2147024894), None)
com_error                                 Traceback (most recent call last)
<ipython-input-64-c7dea54d9247> in <module>()
      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)
com_错误回溯(最近一次调用)
在()
5.尝试:
---->6 email=outlook.OpenSharedItem(文件名[1])
7 emailContent=email.Subject+“\n”+email.Body
OpenSharedItem中的~\Anaconda3\lib\site packages\win32com\client\dynamic.py(self,Path)
com_错误:(-2147352567,‘发生异常’,(4096,‘Microsoft Outlook’,“我们无法打开'2013 Self Cert.msg'。可能该文件已经打开,或者您没有打开该文件的权限。\n\n若要检查您的权限,请右键单击该文件文件夹,然后单击“属性”。,无,0,-2147287038),无)

我无法找出这个错误背后的原因。有人能帮我吗?

将MSG文件复制到一个临时文件夹/文件,其完整路径短于MAX_path。

1。我需要原始的源目录层次结构在目标目录2中复制相同的结构。此外,一些文件名的长度为260个字符,这非常好-一次处理一个消息:您知道原始文件名和文件夹,临时消息文件仅用于OpenSharedItemHanks Dmitry,我现在可以在临时文件夹的帮助下复制所有文件。但是,没有其他方法可以解决上述错误而不需要将所有文件复制到临时文件夹吗?我看不到其他方法。