Reputation: 629
I have a directory composed of 3 folders :
----- 001
----- 002
----- 003
I'd like to create 3 archives named 001.zip, 002.zip and 003.zip. Each archive has to be composed of the content of the folder.
I use the zipfile library and I managed to make an archive of one folder :
import os, zipfile
zf = zipfile.ZipFile('C:/zipfile4.zip', mode='w')
for dirpath,dirs,files in os.walk("C:/Test/002"):
for f in files:
fn = os.path.join(dirpath, f)
zf.write(fn)
But I don't know how to make to create many archives in a row, using that zipfile library.
Upvotes: 0
Views: 80
Reputation: 8774
Using your code sample as a basis, this should work:
import os
import zipfile
dir = "C:/Test/002"
# get list of all subdirectories in the directory
subdirs = [subdir for subdir in os.listdir(dir) if os.path.isdir(os.path.join(dir, subdir))]
# create a zip archive for each subdirectory
for subdir in subdirs:
zf_name = "C:/" + subdir + ".zip"
zf = zipfile.ZipFile(zf_name, mode='w')
for dirpath,dirs,files in os.walk(os.path.join(dir, subdir)):
for f in files:
fn = os.path.join(dirpath, f)
zf.write(fn)
Upvotes: 1