When python file directory does not exist, automatically generated

 1 #!/usr/bin/python
 2 # _*_coding:utf-8_*_
 3 import os
 4 import datetime
 5 
 6 def getDirName():
 7     # 年-月-日 时:分:秒
 8     now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 9     #
10     year_time =datetime.datetime.now().strftime('%Y')
11     # 年-月
12     month_time = datetime.datetime.now().strftime('%Y-%m')
 13      # - in - day 
14      . Datetime.datetime.now daytime = () the strftime ( ' % Y-M-% D% ' )
 15      # : minutes: seconds 
16      hourtime datetime.datetime.now = (). the strftime ( " % H:% M:% S " )
 . 17      Print (now_time + " \ n- " + daytime + " \ n- " + hourtime)
 18 is  
. 19      # get the current working directory, i.e., the directory path of the current Python script work: os .getcwd () 
20 is      pwd The os.getcwd = () + " \\ " + year_time + " \\ "month_time + + " \\ " + daytime
 21 is      # Print (pwd) 
22 is      # file path 
23 is      word_name = os.path.exists (pwd)
 24      # determines whether a file exists: Create absence 
25      IF  Not word_name:
 26 is          os.makdirs (pwd )
 27          Result = { " Status " :. 1, " MSG " : " file directory does not exist, has been automatically generated! " }
 28      the else :
 29          Result = { " Status " : 0," MSG " : " file already exists! " }
 30      return Result
 31 is  IF  the __name__ == ' __main__ ' :
 32      TT = getDirName ()
 33 is      Print (TT)

 

Guess you like

Origin www.cnblogs.com/xh0203/p/12652411.html