Create a folder in Python of Current date format

If you’d need to create a folder for current date in format as YYMMDDHH using python – You can use below code. This will create folder with “00” in hour if the time is between 00:00 to 12:00PM and from 12PM till 00:00AM, it will created folder with “12”.

For 2022-06-26 09:00:00 – It will create folder with name 2022062600.

For 2022-06-26 14:00:00 – It will create folder with name 2022062612.

#! usr/bin/python
from datetime import datetime
import os

today = datetime.now()

if today.hour < 12:
    h = "00"
else:
    h = "12"

os.mkdir("/home/xxx/" + today.strftime('%Y%m%d')+ h)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s