'Treating a url object as a literal

I am developing a script for extracting outlook attachments, the code for a specific file is as such:

msg = outlook.OpenSharedItem(r'C:\Users\AA\Desktop\November2021\November 01_ 2021.msg')

However, I want the script to parse all outlook messages in a folder, and my challenge is how to make the url a literal when it is defined as an object.

file_names = glob.glob(os.path.join(source_dir, '*.msg'))

for file in file_names:
        print (file)
        msg=outlook.OpenSharedItem(file)
        for att in msg.Attachments:
                print(att.FileName)
                print(msg.Attachments.Count)
                att.SaveASFile(os.path.join(r'C:\Users\AA\Desktop\MR\Attachments',
                                            str(att.FileName)))

When I write the code as such, I get the following error:

pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'C://Users/AA/Desktop/November2021/November 01...'. 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)

I am assuming this problem is due to the url path not being prefixed by r'[url object]', I don't know how to make a url path object a literal. Would appreciate some help with the matter.

Edit: Changed the word suffix to prefix Edit 2: This is the full trace back:

Traceback (most recent call last):
  File "C:\Users\AA\PycharmProjects\ARCHIVE\Attachment Extractor\main.py", line 23, in <module>
    msg= outlook.OpenSharedItem(file)
  File "<COMObject GetNamespace>", line 2, in OpenSharedItem
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', "We can't open 'C://Users/AA/Desktop/November2021/November 01...'. 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)

Edit 3: Turns out the problem is with the path, it is being saved with "/" instead of " \ ", how do I fix this?

Edit 4: The following code worked perfectly:

import win32com.client
import os

path = r'C:\Users\AA\Desktop\November2021'
files = [f for f in os.listdir(path) if '.msg' in f]
for file in files:
    outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
    msg = outlook.OpenSharedItem(os.path.join(path, file))
    att=msg.Attachments
    for i in att:
        i.SaveAsFile(os.path.join(r'C:\Users\AA\Desktop\MR\Attachments', i.FileName))

However, I don't want the path to be hard coded and have to change it each time manually. I want to use the following code to select the folder:

# Identify the source file
root = Tk()
root.withdraw()

root.attributes('-topmost', True)


source_dir = filedialog.askdirectory()

file_names = glob.glob(os.path.join(source_dir, '*.msg'))
for file in file_names:
.
.
.

And then carry on from there.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source