Creating a new folder with a new name every 3 months?

Viewed 44

Lets say I have a program that will run every 3 months, I want it to create a new folder with the name 2022Quarter1. Then when I run it again 3 months later, I want it to say 2022Quarter2, etc. Then when a new year comes in, that new year first quarter will be called 2023Quarter1, and so on.

I wrote this part of the program, just so I have an idea of what I want my end goal to be:

import os

for i in range(4):
    i+=1
    num=i
    
try:
    os.makedirs(f'C:/users/desktop/quarters/{yearNum}Quarter{num}')
except FileExistsError:
    # directory already exists
    pass
1 Answers

You can get around this by using Python's datetime module:

  • Current date: current_date = datetime.datetime.today()
  • Current year: current_year = current_date.year
  • Current quarter: current_quarter = 1 + (current_date.month - 1) // 3
Related