title: Adding folders to a zip file using python
body: <p>I want to create a zip file. Add a folder to the zip file and then add a bunch of files to  that folder.</p>
  
  <p>So I want to end up with a zip file with a single folder with files in.</p>
  
  <p>I dont know if its bad practice to have folders in zip files or something but google gives me nothing on the subject.</p>
  
  <p>I started out with this:</p>
  
  <pre><code>def addFolderToZip(myZipFile,folder):
      folder = folder.encode('ascii') #convert path to ascii for ZipFile Method
      for file in glob.glob(folder+"/*"):
              if os.path.isfile(file):
                  print file
                  myZipFile.write(file, os.path.basename(file), zipfile.ZIP_DEFLATED)
              elif os.path.isdir(file):
                  addFolderToZip(myZipFile,file)
  
  def createZipFile(filename,files,folders):
      curTime=strftime("__%Y_%m_%d", time.localtime())
      filename=filename+curTime;
      print filename
      zipFilename=utils.getFileName("files", filename+".zip")
      myZipFile = zipfile.ZipFile( zipFilename, "w" ) # Open the zip file for writing 
      for file in files:
          file = file.encode('ascii') #convert path to ascii for ZipFile Method
          if os.path.isfile(file):
              (filepath, filename) = os.path.split(file)
              myZipFile.write( file, filename, zipfile.ZIP_DEFLATED )
  
      for folder in  folders:   
          addFolderToZip(myZipFile,folder)  
      myZipFile.close()
      return (1,zipFilename)
  
  
  (success,filename)=createZipFile(planName,files,folders);
  </code></pre>
  
  <p>Taken from: <a href="http://mail.python.org/pipermail/python-list/2006-August/396166.html" rel="noreferrer">http://mail.python.org/pipermail/python-list/2006-August/396166.html</a></p>
  
  <p>Which gets rid of all folders and puts all files in the target folder (and its subfolders) into a single zip file. I couldnt get it to add an entire folder.</p>
  
  <p>If I feed the path to a folder in myZipFile.write, I get</p>
  
  <blockquote>
    <p>IOError: [Errno 13] Permission denied: '..\packed\bin'</p>
  </blockquote>
  
  <p>Any help is much welcome.</p>
  
  <p>Related question: <a href="https://stackoverflow.com/questions/296499/how-do-i-zip-the-contents-of-a-folder-using-python-version-2-5">How do I zip the contents of a folder using python (version 2.5)?</a></p>