Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active December 5, 2024 05:34
Show Gist options
  • Save simonthompson99/362404d6142db3ed14908244f5750d08 to your computer and use it in GitHub Desktop.
Save simonthompson99/362404d6142db3ed14908244f5750d08 to your computer and use it in GitHub Desktop.
[Zip to temporary file] Make a temporary zip file and write to it #python #file_operations
import tempfile
import zipfile
# make archive.zip in temp directory
tmpdir = tempfile.mkdtemp()
zip_fn = os.path.join(tmpdir, 'archive.zip')
zip_obj = zipfile.ZipFile(zip_fn, 'w')
for f in files:
zip_obj.write(f, os.path.basename(f)) # add file to archive, second argument is the structure to be represented in zip archive, i.e. this just makes flat strucutre
zip_obj.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment