I have a date in string to check str_last_exported_date.
If the last exported date is current month need to create current month folder . for example if last exported date is 02/15/2025 that is february. It should create Feb folder.
If the last exported date is 01/15/2025 it should create folder Jan. How my logic will be
To create a folder based on the last exported date:
Convert the string date to a datetime object.
Extract the month name using strftime(‘%b’) (e.g., ‘Jan’, ‘Feb’).
Check if the folder exists; if not, create it using os.makedirs().
Example code:
from datetime import datetime
import os
def create_folder(str_last_exported_date):
date = datetime.strptime(str_last_exported_date, '%m/%d/%Y')
month = date.strftime('%b')
folder_path = f'./{month}'
os.makedirs(folder_path, exist_ok=True)
create_folder("02/15/2025") # Creates 'Feb' folder
create_folder("01/15/2025") # Creates 'Jan' folder
This logic will create a folder like “Feb” for February and “Jan” for January.