I recently had a need to make a simple application that created a series of folders, which normally isn't hard, but I had one requirement, which was to make the folder's create and modification dates specific.

So, how to create the folders. In python, it's simple enough to just use the os library's mkdir command:

os.mkdir('FOLDERNAME')

But how about modifying the dates? fortunately, there is the os library's utime function:

os.utime(f'FOLDER', (time,time))

the second parameter takes in an access and modification time. Notice that this does not modify the creation time... modification of the create time does not appear to be something i can do generically, but I should be able to do with the win32 library on the Windows platform, but I'll dicuss that in another article.

With the above knowledge, we can do some date/time work to make a simple program that creates a series of folders for a date range:

import datetime
import time
import os

# Create a datetime object with a specific date
start_date = datetime.date(2024, 1, 1)

# Create a list of 30 days from the start date
date_list = [start_date + datetime.timedelta(days=x) for x in range(0, 30)]

# Create a folder for each date in the list
for date in date_list:
    print(f'Creaeting test_folders/test_folder_{date}')

    # convert datetime object to epoch time
    t = time.mktime(date.timetuple())

    # create folder with the date in the name.
    os.mkdir(f'test_folders/test_folder_{date}')

    # change the folder's access and modification time to the specified date
    os.utime(f'test_folders/test_folder_{date}', (t, t))