3

If I know the path of the directory, how can I zip separately all the folders in it? I tried something, but since I don't fully understand how the os module works, there's not much I can do.

import os, zipfile

directory_path = str(raw_input())
for folder in os.listdir(directory_path):
    zip_file = zipfile.ZipFile(folder + '.zip', 'w')
    for root, dirs, files in os.walk(directory_path+'/'+folder):
        for file in files:
            zip_file.write(os.path.join(root, file),file)
    zip_file.close()

The problem is that it only zips one folder from the directory.

Example:

Directory
 |
 +-- folder1
 |  |  
 |  \-- file 1.1
 |
 +-- folder2
 |  |  
 |  \-- file 2.1
 |    
 +-- folder3
 |  |  
 |  +-- file 3.1
 |  \-- file 3.2

What I want to get is folder1.zip (contains file 1.1), folder2.zip (contains file 2.1) and folder2.zip (contains file 3.1 and file 3.2

Any help is appreciated.

  • Your code looks fine to me. Do you have multiple folders inside the directory you are providing? Use `zip_file.write(os.path.join(root, file),file)` to prevent creating the extra directories. – Mahesh Dec 08 '15 at 10:13
  • @Mahesh I do have multiple folders. Thank you, it zips correctly now, but it still zips only one folder in the directory. –  Dec 08 '15 at 10:20
  • Could you provide an example of the input directory and the desired output? – Daniele Pantaleone Dec 08 '15 at 10:21
  • @DanielePantaleone I've added an example, hope this is what you've meant. –  Dec 08 '15 at 10:26
  • Can you provide the definition for `r` regex? – Lav Dec 08 '15 at 10:29
  • @Lav Sorry, forgot to delete that. –  Dec 08 '15 at 10:32
  • @shomz Sorry, I still do not understand your problem. Can you elaborate what you mean by *zip separately*? Your example also seems to show the input, not the output. As output, I'd expect something like *zipfile1.zip contains folder1/file1, folder2/file2, zipfile2.zip contains folder2/file2, folder3/file3* etc. – phihag Dec 08 '15 at 10:32
  • @phihag I've added what you've asked for. :) –  Dec 08 '15 at 10:36
  • @shomz Thanks! I'm still a little bit puzzled, because your current code seems to do precisely what you want. For example, `folder1.zip` contains `file1.1` for me just fine. – phihag Dec 08 '15 at 10:41
  • @phihag Does the for loop go over every folder in the directory? Because I get folder1.zip, but not folder2.zip and folder3.zip –  Dec 08 '15 at 10:45
  • @shomz I do get `folder2.zip` as well as `folder3.zip` in my tests, and I see nothing wrong with the loop. – phihag Dec 08 '15 at 10:54

1 Answers1

5

I think that the problem is that you are specifying a different arcname for every file in the zip archive (2nd parameter of the write method). Try the following (I also replaced some code like paths joining using os.path module instead of string concatenation):

import os
import zipfile

path = raw_input('Enter the directory: ')
path = os.path.abspath(os.path.normpath(os.path.expanduser(path)))
for folder in os.listdir(path):
    zipf = zipfile.ZipFile('{0}.zip'.format(os.path.join(path, folder)), 'w', zipfile.ZIP_DEFLATED)
    for root, dirs, files in os.walk(os.path.join(path, folder)):
        for filename in files:
            zipf.write(os.path.abspath(os.path.join(root, filename)), arcname=filename)
    zipf.close()
Daniele Pantaleone
  • 2,657
  • 22
  • 33
  • This zips each file separately as I wanted, but it contains every folder in the path, instead of just the files. –  Dec 08 '15 at 10:52
  • Could you please be more clear? Do you mean that `folder1.zip` contains the directory `folder1` with all the files in it, instead of just the files? – Daniele Pantaleone Dec 08 '15 at 10:55
  • What I mean is that if the path is /Users/shomz/Documents/directory, then the folder1.zip contains a folder users and then in it shomz, then directory and _then_ the folder I need. –  Dec 08 '15 at 10:58
  • Ok, now I understood the problem :). I edited the answer. The problem was indeed in the arcname. For more info check this SO question: http://stackoverflow.com/q/7007868/3477005 – Daniele Pantaleone Dec 08 '15 at 11:17