Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Outlook Python 3.4-win32com更改工作目录_Outlook_Python 3.4_Email Attachments_Win32com_Working Directory - Fatal编程技术网

Outlook Python 3.4-win32com更改工作目录

Outlook Python 3.4-win32com更改工作目录,outlook,python-3.4,email-attachments,win32com,working-directory,Outlook,Python 3.4,Email Attachments,Win32com,Working Directory,我有一个简单的程序,它使用win32com.client的Dispatch连接到outlook电子邮件框,浏览电子邮件并将附件保存到本地 如果我想保存附件,我会处理它的路径和文件名,这样我就可以用attachment.SaveAsFile(file)方法在本地保存它。在某些情况下,路径+文件名超过255个字符的限制,因此此操作失败 我想知道如何更改该方法的工作目录。如果我尝试将路径和文件名分开,请使用os.chdir()将我的工作目录更改为EXTRACTED路径,并使用SaveAsFile()仅

我有一个简单的程序,它使用win32com.client的Dispatch连接到outlook电子邮件框,浏览电子邮件并将附件保存到本地

如果我想保存附件,我会处理它的路径和文件名,这样我就可以用attachment.SaveAsFile(file)方法在本地保存它。在某些情况下,路径+文件名超过255个字符的限制,因此此操作失败

我想知道如何更改该方法的工作目录。如果我尝试将路径和文件名分开,请使用os.chdir()将我的工作目录更改为EXTRACTED路径,并使用SaveAsFile()仅将文件名保存在临时文件夹中(这意味着os.chdir()不会影响该方法)

临时文件夹(如果有帮助): C:\Users[用户名]\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.Outlook\GKSDWYZ

那么,如何更改SaveAsFile()方法的默认工作目录

这就是我正在做的:

def save_attachment_as_file(att, file):
    """
    att - an email attachment
    file - to full path and filename to save the attachment as
    """

    # seperate the path and filename
    file_path = file[:file.rfind("\\")]
    file_name = file[file.rfind("\\")+1:]

    # check that both seperate lengths does not exceed the limit
    if not check_length(file_path) or not check_length(file_name):
        return

    # save the currect working directory
    working_path = getcwd()
    # change the working directory to the provided path
    chdir(file_path)
    # save the attachment
    att.SaveAsFile(file_name)
    # change back the working directory
    chdir(working_path)

    print("Created:", file)

我认为真正的问题是,当路径超过255个字符时,如何让Outlook保存附件。根据Microsoft office网站上的信息(例如,它适用于Excel,但本质与您的问题相同,因此“尝试的内容”适用于此处),您的最佳选择是让Outlook保存到临时文件夹,然后使用Python shutil.copyfile()复制文件(因为此函数尊重当前工作目录)

您也可以尝试上述8.3格式

另外,还可以查看和了解可能有用的技巧