首页 > 解决方案 > 如何在 Vscode 中访问另一个文件夹中的文件?

问题描述

所以我的项目中有2个文件夹。Streamlit 文件夹和数据文件夹。我在 Streamlit 中拥有的程序需要访问数据文件夹中的图像。我如何访问这些?

我试过

streamlit.image("data/image.jpeg", width=680)

这抛出了 No such file or directory: 错误。

标签: pythonfilestreamlit

解决方案


当我编写一个脚本来向收件人发送一堆带有不同图像的电子邮件时,我确实需要类似的东西。我所做的是:

#jpg 可以是 tif 或任何其他文件类型。我使用 *.tif,因为我需要一个图像列表。

import glob
target_folder = glob.glob("C:\\Users\\Username\\Destination\\Folder\\filename.jpg")

def open_image_attachment():
    _image_list = []

    for _one_image_attachment in target_folder:
        _attachment_ = open(_one_image_attachment, 'rb')
        _file_name = os.path.basename(_one_image_attachment)
        _part = MIMEBase('application','octet-stream')
        _part.set_payload(_attachment_.read())
        _part.add_header('Content-Disposition',
                         'attachment', 
                         filename = _file_name)
        _image_list.append(_part)
    return _image_list

推荐阅读