使用 Python 从 Outlook 下载同名文件时出现问题。文件夹中只显示一个文件
Having issue with downloading files with same name from outlook using Python. Only one file is showing on folder
from datetime import date
import os
import email
import win32com.client
import pathlib
import glob
import re
from pathlib import Path
path = os.path.expanduser(file_location + "/" +date_file)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) #from inbox
inbox = inbox.Folders['folder 1'] #Folder inside Inbox Folder
today = datetime.date.today() #from todays date
received_dt = date.today() - timedelta(days=1)
messages = inbox.Items
def saveattachments(): # subject= subject line t0 be inserted here.
for message in messages:
if message.Unread:# and message.Senton.date() == today:
for attachment in message.Attachments:
print(attachment.FileName) # shows the file name to be downloaded
attachment.SaveAsFile(os.path.join(path, str(attachment))) #saves it the desired location
if message.Unread:
message.Unread = False #returns the email to read status
saveattachments()
我想下载未读邮件中的所有文件。但是同名文件被替换了,只显示一个文件名。
示例-
Files from outlook
filename which gets saved on desk folder
a_file
a_file
b_file
b_file
c_file
c_file
a_file
d_file
b_file
b_file
a_file
b_file
c_file
d_file
这是意料之中的行为。 There can be only one 特定文件夹中具有特定名称的文件。您将需要实现一些逻辑来查看文件是否已经存在,然后重命名它。
下面是一个简单的例子:
import os
def autorename(filename):
"""recursive function to add " (copy)" to a filename if it already exists.
"""
if os.path.isfile(filename):
newfilename = f"{os.path.splitext(filename)[0]} (copy){os.path.splitext(filename)[1]}"
return autorename(newfilename)
else:
return filename
关于 os.path.splitext()
部分,请参阅 this answer。
编辑
要将此添加到您的代码中,您需要更改此行:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
对此:
attachment.SaveAsFile(autorename(os.path.join(path, str(attachment))))
所以如果文件已经存在它会生成一个新的文件名
from datetime import date
import os
import email
import win32com.client
import pathlib
import glob
import re
from pathlib import Path
path = os.path.expanduser(file_location + "/" +date_file)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6) #from inbox
inbox = inbox.Folders['folder 1'] #Folder inside Inbox Folder
today = datetime.date.today() #from todays date
received_dt = date.today() - timedelta(days=1)
messages = inbox.Items
def saveattachments(): # subject= subject line t0 be inserted here.
for message in messages:
if message.Unread:# and message.Senton.date() == today:
for attachment in message.Attachments:
print(attachment.FileName) # shows the file name to be downloaded
attachment.SaveAsFile(os.path.join(path, str(attachment))) #saves it the desired location
if message.Unread:
message.Unread = False #returns the email to read status
saveattachments()
我想下载未读邮件中的所有文件。但是同名文件被替换了,只显示一个文件名。
示例-
Files from outlook | filename which gets saved on desk folder |
---|---|
a_file | a_file |
b_file | b_file |
c_file | c_file |
a_file | d_file |
b_file | |
b_file | |
a_file | |
b_file | |
c_file | |
d_file |
这是意料之中的行为。 There can be only one 特定文件夹中具有特定名称的文件。您将需要实现一些逻辑来查看文件是否已经存在,然后重命名它。
下面是一个简单的例子:
import os
def autorename(filename):
"""recursive function to add " (copy)" to a filename if it already exists.
"""
if os.path.isfile(filename):
newfilename = f"{os.path.splitext(filename)[0]} (copy){os.path.splitext(filename)[1]}"
return autorename(newfilename)
else:
return filename
关于 os.path.splitext()
部分,请参阅 this answer。
编辑
要将此添加到您的代码中,您需要更改此行:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
对此:
attachment.SaveAsFile(autorename(os.path.join(path, str(attachment))))
所以如果文件已经存在它会生成一个新的文件名