Reputation: 379
I'm trying to get a list of all folders' names I see in the outlook email box, by using the following code:
import win32com.client
def AllFolders(folders):
my_list = []
for folder in folders:
AllFolders(folder.Folders)
print(folder.name)
my_list.append(folder.name)
return my_list
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
user = outlook.Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress
z = AllFolders(outlook.Folders[user].Folders)
My problem is: if I remove the return
I can see all the folders returned by the print
, but if try to return a list of every folder (basically store what I see in the print
in a list), I get z
which is only the first element printed.
How can make a list of every folder name?
Upvotes: 0
Views: 2376
Reputation: 3823
You simply need to concatenate to your list the one that's being returned by the recursive call.
I also moved the call (and the print()
line) after the my_list.append()
line just to have the subfolders added after their parent.
import win32com.client
def AllFolders(folders):
my_list = []
for folder in folders:
print(folder.name)
my_list.append(folder.name)
my_list += AllFolders(folder.Folders)
return my_list
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
user = outlook.Application.Session.CurrentUser.AddressEntry.GetExchangeUser().PrimarySmtpAddress
z = AllFolders(outlook.Folders[user].Folders)
Upvotes: 2