3

I have been trying to add multiple python modules into a zip file. However, I am unsuccessful as the the newly added module is replacing the previous one and I don't understand the relation. command_utils adds a util file and the next command_utils2 adds another module after which the entire first module is gone. Basically I want to add 2 of my modules to a zip file after making the zip file out of source code. Here is my code.

import shutil
import os
import subprocess

zip_name = os.getcwd().split("/")[-1]

project_dir = '/tmp/'

shutil.make_archive(zip_name, "zip", project_dir+"test/")

os.chdir('/tmp/')
command_utils = 'zip -r '+project_dir+'test/'+zip_name+'platformutils'
print os.getcwd()
command_utils2 = 'zip -r '+project_dir+'test/'+zip_name+' pytz'
command_delete_archive = 'zip -d '+project_dir+'test/'+zip_name+'.zip '+zip_name+'.zip'
# command_update_function = 'aws lambda update-function-code --function-name 
'+zip_name+' --zip-file fileb://'+project_dir+zip_name+'/'+zip_name+'.zip'
# print command_utils
print command_utils2
print command_delete_archive
# print command_update_function
try:
   # c_u = subprocess.Popen(command_utils, shell=True, stdout=subprocess.PIPE)
   c_u2 = subprocess.Popen(command_utils2, shell=True, stdout=subprocess.PIPE)
   c_d_a = subprocess.Popen(command_delete_archive, shell=True, stdout=subprocess.PIPE)
   # p = subprocess.Popen(commands
except subprocess.CalledProcessError as e:
   raise e
ZZzzZZzz
  • 1,800
  • 3
  • 29
  • 51
  • 1
    Possible duplicate of [How to create a zip archive of a directory](https://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory) – Paco H. Jun 02 '17 at 19:08
  • paco H. No, I have seen that question. Mine is about add few folders to already existing zip file. i want folder1 and folder2 to be added to a zip file myZip.zip which was already zipped with few files earlier. I basically wanna add folders to this zip in real time. – ZZzzZZzz Jun 02 '17 at 19:56
  • 1
    Then maybe this one? https://stackoverflow.com/questions/34270582/adding-file-to-existing-zipfile – Paco H. Jun 02 '17 at 19:58
  • This helped but the below answer is has given me a right solution. – ZZzzZZzz Jun 05 '17 at 17:52

1 Answers1

4

Using zipfile module:

from zipfile import Zipfile
myzipfile = ZipFile("spam.zip", mode='a')
for mod_path in module_paths: 
    myzipfile.write(mod)
myzipfile.close()

Notice I used a mode for the zip file not w:

If mode is 'a' and file refers to an existing ZIP file, then additional files are added to it. If file does not refer to a ZIP file, then a new ZIP archive is appended to the file. This is meant for adding a ZIP archive to another file (such as python.exe). If mode is 'a' and the file does not exist at all, it is created.

GIZ
  • 4,409
  • 1
  • 24
  • 43
  • This helped and this command did the trick for me zip.write(path_to_file, os.path.basename(path_to_file)) – ZZzzZZzz Jun 05 '17 at 17:51
  • 3
    Thanks, it worked! Should be ZipFile at the importing statement and mod_path into for loop ;) – oz19 Mar 09 '21 at 09:20