Python method for creating multilevel catalogs by current date (year, month, day)

Source: Internet
Author: User
Tags python script timedelta

First look at the actual effect, now time 2018.4.26

Using a Python script to generate a multilevel catalog by month and day, the created directory can be easily consulted by placing the system-generated log files in it, with the following code:

#!/usr/bin/env python#Coding=utf-8Import TimeImportOS#string that gets the current system timeLocaltime=time.strftime ('%y-%m-%d%h:%m:%s', Time.localtime (Time.time ()))Print('localtime='+localtime)#System Current Time yearYear=time.strftime ('%Y', Time.localtime (Time.time ()))#MonthMonth=time.strftime ('%m', Time.localtime (Time.time ()))#DateDay=time.strftime ('%d', Time.localtime (Time.time ()))#time-specific hours, minutes, millisecondsMdhms=time.strftime ('%m%d%h%m%s', Time.localtime (Time.time ())) Fileyear=OS.GETCWD () +'/upload_files/'+ '/' +Yearfilemonth=fileyear+'/'+Monthfileday=filemonth+'/'+ Dayif  notos.path.exists (fileyear): Os.mkdir (fileyear) os.mkdir (filemonth) os.mkdir (fileday)Else:    if  notos.path.exists (filemonth): Os.mkdir (Filemonth) os.mkdir (fileday)Else:        if  notos.path.exists (fileday): Os.mkdir (Fileday)#create a file with ' Timefile_ ' + specific time for the file namefiledir=fileday+'/timefile_'+mdhms+'. txt' out=open (Filedir,'W')#writes the current system time string to the fileOut.write ('localtime='+localtime) out.close ()

Additional knowledge points about date and time

Import= Datetime.date.today ()

I want to make it this way.

Importdatetime#This is the designation 2008/12/5 23:59:59Today = Datetime.datetime (2008, 12, 5, 23, 59, 59) #datetime can also be added minus, 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 (6, 0, 0) #One plus 23 hours, 59 minutes, 59 seconds .x = Datetime.timedelta (hours = all, minutes = seconds = 59) W= W +x#w = datetime.datetime (6 , +,--) 

What's more, if you want to get today's year, month, day is also very simple to say

Import# now in the time #x = Datetime.datetime (5, (+), #指定時間)  #   will get the # # will get # # will get 5x.hour   #  # points # seconds

Python method for creating multilevel catalogs by current date (year, month, day)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.