我是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)
如果我重命名文件名,使绝对路径长度减少到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)
<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)
我无法弄清楚这个错误背后的原因。有人可以帮助我吗?
答案 0 :(得分:1)
将MSG文件复制到临时文件夹/文件,其完整路径短于MAX_PATH。