Skip to content
Advertisement

How to json dump inside Zipfile.open process?

I am trying to write a json inside a ZipFile BytesIO process. It goes like this:

JavaScript

It is later saved in a Django File field. However it does not dump the data into the json_file. Finds it hard since it does not report an error message.

Advertisement

Answer

Your code ‘shadows’ zipfile, which won’t be a problem by itself, but would cause problems if you needed zipfile later in your code. In general, don’t shadow standard library identifiers and Python keywords.

Why it’s a problem, I don’t know, but it appears json.dump expects something from the file pointer that the file-like object that ZipFile.open() gets you doesn’t have.

This is how to get around that:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement