如何从电子邮件中下载文件并将其保存在C:\

问题描述

我有附件信息(“ contentBytes”:“ iVBORw0KGgoAAAANSUhEUgAAAOEAAADhCAMAAAAJbSJIAAAArlBMVEX ...等)(来自图形API请求),下面是我用来对其进行转换的代码,这是成功的,但是我需要将其保存到C盘中。是否还有其他需要添加内容?或者我应该朝另一个方向前进?

import base64
imgdata = base64.b64decode(contentBytes)
filename = "sample.png"
with open(filename,'wb') as f:
    f.write(imgdata)

解决方法

如果要更改保存位置,只需将路径和文件名一起写 顺便说一句,由于未定义contentBytes,因此代码会出错,因此代码无法正常工作

import base64

imgdata = base64.b64decode(contentBytes)

filename = "C:/users/user/path/to/save/file/to/sample.png"

with open(filename,'wb') as f:
   f.write(imgdata)

这应将文件保存到所选目录