How can I zip texts and images?

Hello!

I'm currently writing a Python script in which I have some text (as a string, not as a file) and images (as bytes, again not as a file), and I want to zip them all together. I know how to zip saved files using the Zipfile module. But I haven't saved the files; I've only saved them as strings or bytes objects.

Does anyone have any idea how to do this without temporarily saving the files? That would be a huge help!

Thank you in advance!

LG

(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jo135
1 year ago

This goes directly with the built-in zipfile:

https://docs.python.org/3/library/zipfile.html#zipfile.ZipFile.open

Example:

import zipfile

with zipfile.ZipFile('bla.zip', mode='w') as zip:
  with zip.open('test.txt', mode='w') as myfile:
    myfile.write(bytes('hello, world', 'utf-8'))