StringIO.getvalue return content of StringIO:

>>> import StringIO
>>> f = StringIO.StringIO()
>>> f.write('asdf')
>>> f.getvalue()
'asdf'

Alternatively, you can change position of the file using seek:

>>> f.read()
''
>>> f.seek(0)
>>> f.read()
'asdf'

Try following:

mf = StringIO.StringIO()
with zipfile.ZipFile(mf, mode='w', compression=zipfile.ZIP_DEFLATED) as zf:
    zf.writestr('file1.txt', "hi")
    zf.writestr('file2.txt', "hi")

with open("C:/path/my_zip.zip", "wb") as f: # use `wb` mode
    f.write(mf.getvalue())

Answer from falsetru on Stack Overflow
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › zipfile.ZipFile.writestr.html
zipfile.ZipFile.writestr — Python Standard Library
zipfile.ZipFile.writestr · View page source · ZipFile.writestr(zinfo_or_arcname, bytes, compress_type=None)[source]¶ · Write a file into the archive.
🌐
O'Reilly
oreilly.com › library › view › python-in-a › 0596001886 › re281.html
writestr - Python in a Nutshell [Book]
Namewritestr Synopsis z.writestr(zinfo,bytes) zinfo must be a ZipInfo instance specifying at least filename and date_time. bytes is a string of bytes. writestr adds a member to... - Selection from Python in a Nutshell [Book]
🌐
GitHub
gist.github.com › 18a675276af53a8ef630
zipfile writestr · GitHub
zipfile writestr · Raw · zipfile_writestr.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode ...
🌐
Python
bugs.python.org › issue3394
Issue 3394: zipfile.writestr doesn't set external attributes, so files are extracted mode 000 on Unix - Python tracker
July 17, 2008 - This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/47644
🌐
Stack Overflow
stackoverflow.com › questions › 65333799 › different-file-paths-in-python-zipfile-depending-on-write-vs-writestr
Different File Paths in Python ZipFile Depending on .write() vs .writestr() - Stack Overflow
for root, _, filenames in os.walk(tmpdirname): for root_name in filenames: print(f"Handle zip of {root_name}") name = os.path.join(root, root_name) name = os.path.normpath(name) with open(name, mode='rb') as extracted_file: zipFile.writestr(f'/data/{root_name}', extracted_file.read())
🌐
Python Examples
pythonexamples.org › python-write-string-to-text-file
How to Write String to Text File in Python?
To write string to a Text File: Open file in write mode using open() function. Write string to file using write() method. Close the file using close() method.
Find elsewhere
🌐
Runebook.dev
runebook.dev › en › docs › python › library › zipfile › zipfile.ZipFile.writestr
From String to Zip: Handling Data with Python's writestr() and write()
The zipfile.ZipFile.writestr() method is a convenient way to add a file to a zip archive directly from a string or bytes in memory, without needing to save it to a physical file first.
🌐
GitHub
github.com › python › cpython › issues › 134383
ZipFile.writestr doesn't behave like Path.write_* · Issue #134383 · python/cpython
May 20, 2025 - import zipfile with zipfile.ZipFile("foo.zip", "w") as z: z.writestr("x", ()) print(z.namelist()) # -> "x" Notably, Path.write_text behaves the way I'd expect -- it has to construct a memoryview successfully first before opening the file. from pathlib import Path Path("x").write_bytes(()) # -> neither creates nor truncates · @jaraco I assume ZipPath not supporting writes is intentional? This is a minor feature, which does not need previous discussion elsewhere ... stdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancementA feature request or enhancement
Published   May 20, 2025
Author   thatch
🌐
Python Module of the Week
pymotw.com › 2 › zipfile
zipfile – Read and write ZIP archive files - Python Module of the Week
$ python zipfile_write_arcname.py ... data to a file, then adding that file to the ZIP archive, you can use the writestr() method to add a string of bytes to the archive directly....
🌐
GitHub
github.com › miurahr › py7zr › issues › 290
writestr support just like ZipFile.writestr · Issue #290 · miurahr/py7zr
February 4, 2021 - Describe the solution you'd like in ZipFile, there i got ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)
Author   BioEvo
🌐
Medium
medium.com › @vickypalaniappan12 › create-in-memory-zip-files-in-python-79193fbbc6c3
Create in-memory zip files in Python | by Palaniyappan Ramanathan | Medium
August 16, 2024 - import zipfile from io import BytesIO from fastapi import FastAPI from fastapi.responses import StreamingResponse app = FastAPI() def generate_text_file(filename: str) -> BytesIO: file_content = BytesIO() file_content.write(b"Hello World") file_content.name = f"{filename}.txt" return file_content @app.get("/{username}") def export_user_data(username: str): text_files = [generate_text_file(filename=f"{username}_{i}") for i in range(1, 11)] zip_buffer = BytesIO() with zipfile.ZipFile( file=zip_buffer, mode="w", compression=zipfile.ZIP_DEFLATED, compresslevel=9, ) as zip_archive: for text_file in text_files: zip_archive.writestr(zinfo_or_arcname=text_file.name, data=text_file.getvalue()) zip_buffer.seek(0) return StreamingResponse( zip_buffer, media_type="application/zip", headers={"Content-Disposition": f"attachment; filename={username}.zip"}, )
🌐
Python
docs.python.org › 3 › library › zipfile.html
zipfile — Work with ZIP archives
February 23, 2026 - ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)¶
🌐
GitHub
github.com › python › cpython › issues › 119182
[C API] Add an efficient public PyUnicodeWriter API · Issue #119182 · python/cpython
May 19, 2024 - The writer takes care of the internal buffer kind: Py_UCS1 (latin1), Py_UCS2 (BMP) or Py_UCS4 (full Unicode Character Set). It also implements an optimization if a single write is made using PyUnicodeWriter_WriteStr(): it returns the string unchanged without any copy. Example of usage (simplified code from Python/unionobject.c):
Published   May 19, 2024
Author   vstinner
🌐
Demo2s
demo2s.com › python › python-zipfile-writing-data-to-a-new-archive-with-writestr.html
Python zipfile Writing data to a new archive with writestr().
""" from zipfile_infolist import print_info import zipfile msg = 'This data did not exist in a file.' with zipfile.ZipFile('writestr.zip', mode='w', compression=zipfile.ZIP_DEFLATED, ) as zf: zf.writestr('from_string.txt', msg) print_info('writestr.zip') with zipfile.ZipFile('writestr.zip', 'r') as zf: print(zf.read('from_string.txt')) PreviousNext · Python zipfile Retrieve the contents of an archive member.
🌐
EyeHunts
tutorial.eyehunts.com › home › python zipfile write
Python zipfile write - Tutorial - By EyeHunts
December 8, 2022 - Use the writestr() method to Python zipfile write. It adds a string of bytes to the archive directly. A simple example of code writing the...