如何将变量保存到本地桌面位置

How to save a variable to a local a desktop location

我需要将电子邮件中的消息主题作为一个简单的 txt 文件保存在我的桌面上。 在下面的示例中,subject1 定义了消息主题。 我试过 subject1.SaveAsFile(location) 但没有用。由于我使用的是Jupyter notebook,它保存在本地,但我需要将它保存在我的桌面上。

import win32com.client
import datetime
import os
import email
import pandas as pd
import os
import glob
import file_operations


outlook = win32com.client.dynamic.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.GetDefaultFolder(6)
message = inbox.Items

for message in inbox.Items:
    if message.UnRead == True:
       subject1 = message.Subject

您需要打开一个文件才能这样做。确保 subject1 是一个字符串,或者将其转换为带有 str().

的字符串
with open('C:\Users\ShaneN\Desktop\testing\filename.txt','w') as file:
    file.write(subject1)