Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8197179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:41:20+00:00 2026-06-07T05:41:20+00:00

I have a program that put all files in different folders, e.g. these files:

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:41:21+00:00Added an answer on June 7, 2026 at 5:41 am

    With one zip file per folder:

    >>> for date in dict_date:
    with ZipFile(os.path.join(path, date, '{0}.zip'.format(date)), 'w') as myzip:
        for animal in dict_date[date]:
            for fil in os.listdir(os.path.join(path, date, animal)):
                if fil.endswith('.jpg'):
                    myzip.write(os.path.join(path, date, animal, fil))
    

    This should replace your lines starting from directory = src.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# program that selects data from two different database files and
I have a program that holds many threads, lets put as an example six
I have a program that reads a file, treat it and put the results
I have a program that encrypts files, but adds the extension .safe to the
If I have a java project that consists of several different types of files
I'm writing a program that, using Rijndael, will encrypt and decrypt files/folders using a
I have a program that runs as a Windows Service which is processing files
I have a Swing Java program that reads *.txt files in a resource folder
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.