Home > database >  Get the names of the files created in a particular month and then zip them separately
Get the names of the files created in a particular month and then zip them separately

Time:02-18

I have a folder that have multiple subfolders and files. They contains almost a million files. I need to create zip files for each month, so that they are easy to move. For example, I want to zip all files created in Jan 2021 in Jan_2021.zip, and zip all files created in Feb 2021 in Feb_2021.zip

My problem is that when I get timestamp with ctime, its not something that I can filter. Can anyone help me understand how can I get names of files created in a particular month.

import os,time

src1 = "C:\\Users\\My-Info"
os.chdir(src1)

for file in os.listdir():
print(file   "Time Stamp - "   str(time.ctime(os.path.getctime(file))))

It gives me output like this:

Payslip - Tue Dec 21 12:57:07 2021
Process - Thu Feb 17 20:46:23 2022
Scripts - Tue Dec 21 12:57:07 2021

I am on windows with python 3.

CodePudding user response:

You can create a list containing the name of the file and its creation date (month-year). Then you join in a list the files having the same date.

import os
import datetime

data = [[datetime.datetime.fromtimestamp(os.path.getctime(file)).strftime("%b-%Y"), file] for file in os.listdir()]

final_data = {}
for file in data:
    if file[0] in final_data:
        final_data[file[0]].append(file[1])
    else:
        final_data[file[0]] = [file[1]]
print(final_data)

Output:

{'Dec-2021': ['Payslip', 'Scripts'], 'Feb-2022': ['Process']}

CodePudding user response:

You can groupby() the files on the creation year month:

import itertools

def get_ctime(file):
    return datetime.datetime.fromtimestamp(os.path.getctime(file)).strftime("%Y%m")

files = sorted(os.listdir(), key=get_ctime)
for yyyymm, files in itertools.groupby(files, key=get_ctime):
    print("Files created in ", yyyymm)
    for f in files:
        print(file, time.ctime(os.path.getctime(file)))
    print("")

The get_ctime() function takes a file, gets its ctime and formats it as YYYYMM. groupby() groups consecutive files of the same value from this function together, which is why we need to sort files by the same key before running groupby() on it.

  • Related