Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create folder in python with date YYYYMMDDHH

Tags:

python

I am doing my first steps in python. I try to create a folder with the date with format YYYYMMDDHH

For example, today, 20170225HH, where HH should 00 if the real hour is between 00h-12h, and 12 if the real hour is between 12h-24h.

With the following code I create the folfer but I don't get 00 at 10:00h, I get 12:00?? Any help? I create a folder with name 2017022512 and I need 2017022500 at 10:00...thanks

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

today=time.strftime('%Y%m%d')
hour=time.strftime('%h')
if(hour<12): h = "00"
else: h ="12"
os.system("mkdir /home/xxx/"+str(today)+""+str(h)+"")
like image 491
Enric Agud Pique Avatar asked Feb 06 '23 00:02

Enric Agud Pique


1 Answers

Use below code,

#! 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)
like image 163
shashankqv Avatar answered Feb 08 '23 16:02

shashankqv