I am trying to write a function that will go through multiple zip file full of png's to compile all pictures into a single zip. Each zip has it's contents labeled 0-# of png's so I need to rename all of the files in order then zip them together. I have everything completed with temporary directories, one for renaming rename_dir and one for zipping everything collect_dir. I am running into a problem where only the final zip will be saved, but the names inside will be correct. I think that the collect_dir is being closed in between each iteration and thus deleting everything in it, so when it zips in the end only the final group will be included. Is there a way to keep one directory open (collect_dir) while opening and closing another (rename_dir) (without ever closing collect_dir, at least until the function is finished and zipping is complete)
this is the portion of code i think is relevant
with tempfile.TemporaryDirectory() as collect_dir:
with tempfile.TemporaryDirectory() as rename_dir:
# unpack files into temp directory
shutil.unpack_archive(iter_dir_path, rename_dir)
# bug checking
# print(os.listdir(rename_dir))
for pic in range(len(os.listdir(rename_dir))):
# Get path for pic
old_name = os.path.join(rename_dir, str(pic) + ".png")
# Create new path for pic in collect directory and changing name to pic count
new_name = os.path.join(collect_dir, str(pic_count) + ".png")
# bug checking
# names_old.append(old_name)
# names_new.append(new_name)
# Rename path (moves file)
os.rename(old_name, new_name)
pic_count += 1
# Create final zip
save_path = os.path.dirname(download_path)
final_dir_path = os.path.join(save_path, name_and_volume)
archive = shutil.make_archive(final_dir_path, 'zip', collect_dir)
archive command at the end, not inside a for loop