6

Is it possible to write a folder and its contents to an existing ZipFile? I've been messing around with this for a while and can only manage to write the folder structure to the archive, anything inside the folder isn't copied. I don't want to point to a specific file, because the idea is that the folder contents can change and the program will just copy the whole folder into the archive no matter what is inside.

Currently I have,

myzipfile.write('A Folder\\Another Folder\\') 

but I want the contents of 'Another Folder' to be copied as well not just the empty folder

Hopefully you understand what I mean.

Blaszard
  • 29,431
  • 45
  • 147
  • 228
Artharos
  • 63
  • 1
  • 3
  • possible duplicate of [How to create a zip archive of a directory](http://stackoverflow.com/questions/1855095/how-to-create-a-zip-archive-of-a-directory) – jscs Jul 05 '15 at 05:39

1 Answers1

7

Use os.walk:

import os
for dirpath,dirs,files in os.walk('A Folder/Another folder'):
  for f in files:
    fn = os.path.join(dirpath, f)
    myzipfile.write(fn)
phihag
  • 263,143
  • 67
  • 432
  • 458