Last active
December 5, 2024 05:34
-
-
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
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 characters.
Learn more about bidirectional Unicode characters
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