i am having trouble with tempfile library. As part of a bigger code, i need to open a temporary folder (that needs to be closed at the end). i will dump some files in it. however i need to organize what's inside, so I would need to open a directory inside this temporary folder. I tried with mkdir and makedirs, and before the temporary folder gets deleted i zip everything to check if the directories I created were actually there. apparently not, at the end I get an empty zip file. what is wrong here, why are the directories dir1 and dir2 not created?
import os
import tempfile
import zipfile
with tempfile.TemporaryDirectory() as directory:
try:
os.mkdir(os.path.join(directory, "dir1"))
os.makedirs(os.path.join(directory, "dir2"))
finally:
# zip to check the content created
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file))
zipf = zipfile.ZipFile("final.zip", 'w', zipfile.ZIP_DEFLATED)
zipdir(directory, zipf)
zipf.close()