I have a program that put all files in different folders, e.g. these files:
20120809_1204567_cat_pic1.jpg
20120810_1204567_cat_pic1.jpg
20120811_1204567_cat_pic1.jpg
The program creates 3 different folders with the name of the first number and a subfolder with the name cat. Now I want to zip these folders, but I don’t know the name of each folder before I start the program. How could I zip the folders immediately after the creation of these folders?
import zipfile
import glob
import datetime
import os
from collections import defaultdict
from shutil import copyfile
src = 'D:/Testing/src/'
for name in glob.glob('D:/Testing/src/*'):
print name
dict_date = defaultdict(lambda : defaultdict(list))
for fil in os.listdir(src):
if os.path.isfile(os.path.join(src, fil)):
date, animal = fil.split('_')[0], fil.split('_')[2]
dict_date[date][animal].append(fil)
for date in dict_date:
for animal in dict_date[date]:
try:
os.makedirs(os.path.join(src, date, animal))
except os.error:
pass
for fil in dict_date[date][animal]:
copyfile(os.path.join(src, fil), os.path.join(src, date, animal, fil))
directory = src
os.chdir(directory)
files = glob.glob('*.txt')
for filename in files:
os.unlink(filename)
files2 = glob.glob('*.png')
for filename in files2:
os.unlink(filename)
With one zip file per folder:
This should replace your lines starting from
directory = src.