Look at the actual effect first, the current time is 2018.4.26
Use the python script to generate multi-level directories according to the year, month and day. The created directory can put the log files generated by the system into it for easy reference. The code is as follows:
#!/usr/bin/env python #coding=utf-8 import time import os #Get the string of the current system time localtime=time.strftime( ' %Y-%m-%d %H:%M:%S ' ,time.localtime(time.time())) print ( ' localtime= ' + localtime) #system current time year year=time.strftime( ' %Y ' ,time.localtime(time.time())) #month month =time.strftime( ' %m ' ,time.localtime(time.time ())) #date day =time.strftime( ' % d ' ,time.localtime(time.time())) #specific time hour minute millisecond mdhms=time.strftime( ' %m%d%H%M% S',time.localtime(time.time())) fileYear=os.getcwd()+'/upload_files/'+'/'+year fileMonth=fileYear+'/'+month fileDay=fileMonth+'/'+day if not os.path.exists(fileYear): os.mkdir(fileYear) os.mkdir(fileMonth) os.mkdir(fileDay) else: if not os.path.exists(fileMonth): os.mkdir(fileMonth) os.mkdir(fileDay) else: if not os.path.exists(fileDay): os.mkdir(fileDay) #Create a file with 'timeFile_'+specific time as the file name fileDir=fileDay+ ' /timeFile_ ' +mdhms+ ' .txt ' out =open(fileDir, ' w ' ) #Write the current system time string in the file out.write( ' localtime= ' + localtime) out.close()
Other knowledge points about date and time
import datetime today = datetime.date.today()
If you want to specify the hours, minutes and seconds, you can do this
import datetime #This is specifying 2008/12/5 23:59:59 today = datetime.datetime(2008, 12, 5, 23, 59, 59 ) # datetime can also do addition and subtraction, adding one second at a time x = datetime.timedelta(seconds = 1 ) y = datetime.date(2008, 12, 5, 23, 59, 59) w = x + y #w = datetime.datetime(2008, 12, 6, 0, 0) #Add 23 hours 59 minutes 59 seconds at a time x = datetime.timedelta(hours = 23, minutes = 59, seconds = 59 ) w = w + x #w = datetime.datetime(2008, 12, 6, 23, 59, 59)
Also, if you want to get today's year, month, and day, you can simply say
import datetime x = datetime.datetime.now() #now time # x = datetime.datetime(2008, 12, 5, 23, 59, 59) #specified time x.year #will get 2008 x.month #will get 12 x.day #will get 5 x.hour # hourx.minute # minutex.second # second59 _