Writing png files to a zip archive with Python 3.5 zipfile, getting The parameter is incorrect with winzip

0

In a process almost perfectly described by the question in this post, I am writing png files of matplotlib plots to a BytesIO instance. I am then writing each of those to another BytesIO instance with an instance of ZipFile, calling zipfile.writestr method.

Making the plots

import pandas as pd
import matplotlib.pyplot as plt

def write_plot(data):
    plot_buff = BytesIO()
    fig, ax = plt.subplots()
    dataframe = pd.DataFrame(data)
    dataframe.plot(x="length", y="left", ax=ax, color="b")
    dataframe.plot(x="length", y="right", ax=ax, color="r")
    plt.savefig(plot_buff)
    return plot_buff

Archiving the plots

zip_buff = BytesIO()
with ZipFile(zip_buff, "w") as zipfile:
    for number, data_set in enumerate(data_sets):
        plot = write_plot(data_set)
        zipfile.writestr("{}.png".format(number), plot.getvalue())
with open(file_path, "wb") as write_buff:
    write_buff.write(zip_buff.getvalue())

But the zip archive I get back gives me the error : Error 0x80070057: The parameter is incorrect

It opens fine in 7-zip, but I can't expect my users to know or try that.

Edit: Sorry, the missing "wb" param was a typo in the question, it is part of my actual code.

python
python-3.5
asked on Stack Overflow Oct 9, 2017 by Peter Ogden • edited Oct 10, 2017 by Peter Ogden

1 Answer

0

Apologies, this was a bad example. The issue I was having could not be determined from it and was pretty simple. In the course of building a fully verifiable example, I discovered that the example code does produce png images which can be extracted with WinZip. This gave me a reference point between my actual code which was, slightly different and the example. It turned out that I had put ":" into the file names.

answered on Stack Overflow Oct 10, 2017 by Peter Ogden

User contributions licensed under CC BY-SA 3.0