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

Source: Internet
Author: User
Tags timedelta
This article mainly describes the Python implementation of the current date (year, month, day) to create a multi-level directory of the method, has a certain reference value, now share to everyone, the need for friends can refer to

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 Timeimport os# obtains the current system time string Localtime=time.strftime ('%Y-%m-%d%H:%M:%S ', Time.localtime (Time.time ())) print (' localtime= ' +localtime) #系统当前时间年份year =time.strftime ('%Y ', Time.localtime ( Time.time ())) #月份month =time.strftime ('%m ', Time.localtime (Time.time ())) #日期day =time.strftime ('%d ', time.localtime ( Time.time ())) #具体时间 hour minutes milliseconds mdhms=time.strftime ('%m%d%h%m%s ', Time.localtime (Time.time ())) FILEYEAR=OS.GETCWD () + '/ Upload_files/' + '/' +yearfilemonth=fileyear+ '/' +monthfileday=filemonth+ '/' +dayif 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) #创建一个文件, with ' timefile_ ' + specific time for file name filedir=fileday+ '/timefile_ ' +mdhms+ '. txt ' out=open (filedir, ' W ') # Writes the current system time string Out.write (' localtime= ' +localtime) out.close () in the file

Additional knowledge points about date and time

Import Datetimetoday = Datetime.date.today ()

I want to make it this way.

Import datetime# This is the designation 2008/12/5 23:59:59today = Datetime.datetime (5, A, a, a.) #datetime can also do this by adding a second x = Date Time.timedelta (seconds = 1) y = datetime.date (6, 5, Max, A, A, a) W = x + y#w = datetime.datetime (, 0, 0) #一次 Plus 23 hours, 59 minutes, 59 seconds x = Datetime.timedelta (hours = 12, minutes = seconds) W = w + x#w = Datetime.datetime (2008, 6, 2, 3, 59, 59)

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

Import Datetimex = Datetime.datetime.now () #現在時間 #x = Datetime.datetime ($, 5, +,--) #指定時間x. Year #會拿到 2008x.mon Th #會拿到 12x.day # will get 5x.hour  #時x. Minute #分x. Second #秒 59

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.