小编Ale*_*tto的帖子

在Python中直接从内存中保存多个对象以压缩

我想将循环创建的多个对象(例如图形)直接保存到 zip 文件中,而不将它们保存在目录中。

目前我将这些数字保存在一个文件夹中,然后压缩它们。

    import matplotlib.pyplot as plt
    from zipfile import ZipFile

    for i in range(10):
        plt.plot([i, i])
        plt.savefig('fig_' + str(i) + '.png')
        plt.close()

    image_list = []
    for file in os.listdir(save_path):
        if file.endswith(".png"):
            image_list.append(os.path.join(save_path, file))

    with ZipFile(os.path.join(save_path, 'export.zip'), 'w') as zip:
        for file in image_list:
            zip.write(file)
Run Code Online (Sandbox Code Playgroud)

在肯定回答的情况下,有没有办法对任何类型的对象做同样的事情,还是取决于对象类型?

非常感谢

python zip matplotlib zipfile

5
推荐指数
1
解决办法
1740
查看次数

标签 统计

matplotlib ×1

python ×1

zip ×1

zipfile ×1