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)+"")
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With