0

I would like to create a fresh zip file with multiple entries (files) in it. The point is that the files don't exist regulary. I want to create them on the fly while creating the zip file.

Here the file foobar.txt doesn't exist on the filesystem.

>>> import zipfile
>>> zf = zipfile.ZipFile('x.zip', 'w')
>>> zf.write('foobar.txt')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/zipfile.py", line 1727, in write
    zinfo = ZipInfo.from_file(filename, arcname,
  File "/usr/lib/python3.9/zipfile.py", line 501, in from_file
    st = os.stat(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'foobar.txt'

What I would like to do is something like this

>>> import zipfile
>>> zf = zipfile.ZipFile('x.zip', 'w')
>>> handle = zf.write('foobar.txt')
>>> handle.write('content in foobar file')
>>> handle.close()
>>> zf.close()

I don't want to use the workaround to create that files on filesystem first and then delete them after I added them as zip entries.

buhtz
  • 10,774
  • 18
  • 76
  • 149

0 Answers0