ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python 生成以日期时间命名的目录(应于存放日志/报告文件)

2022-05-05 11:01:53  阅读:196  来源: 互联网

标签:python timeDir 存放 path 日志 now os 目录 dateDir


(FileUtil.py)本模块主要用于获取当前的日期以及时间,用于应于存放日志/报告文件,代码示例如下:

 1 import os
 2 import time
 3 from util import ProjectPathUtil
 4 from datetime import datetime
 5 
 6 
 7 # 输出当前时间格式:年-月-日
 8 def currentDate():
 9     date = time.localtime()
10     # 构造今天的日期字符串
11     today = str(date.tm_year) + "-" + str(date.tm_mon) + "-" + str(date.tm_mday)
12     return today
13 
14 
15 # 输出当前时间格式:时-分-秒
16 def currentTime():
17     timeStr = datetime.now()
18     now = timeStr.strftime("%H-%M-%S")
19     return now
20 
21 
22 # 创建日志目录:log为一级目录,年月日为二级目录,时分秒为三级目录 ,ag:log/2022-5-5/10-30-12
23 def createLogDir():
24     # 获取当前工程的跟目录的绝对路径
25     projectPath = ProjectPathUtil.get_project_path()
26     today = currentDate()
27     dateDir = os.path.join(projectPath, 'log', today)
28     print("日期目录:%s" % dateDir)
29     if not os.path.exists(dateDir):
30         # 如果以今天日期命名的目录不存在则创建
31         os.mkdir(dateDir)
32     now = currentTime()
33     timeDir = os.path.join(dateDir, now)
34     print("时间目录:%s" % timeDir)
35     if not os.path.exists(timeDir):
36         # 如果以今天日期时间命名的目录不存在则创建
37         os.mkdir(timeDir)
38 
39     return dateDir
40 
41 
42 # 创建报告目录:report为一级目录,年月日为二级目录,时分秒为三级目录 ,ag:report/2022-5-5/10-30-12
43 def createReportDir():
44     # 获取当前工程的跟目录的绝对路径
45     projectPath = ProjectPathUtil.get_project_path()
46     today = currentDate()
47     dateDir = os.path.join(projectPath,'report',today)
48     print("日期目录:%s" % dateDir)
49     if not os.path.exists(dateDir):
50         # 如果以今天日期命名的目录不存在则创建
51         os.mkdir(dateDir)
52     now = currentTime()
53     timeDir = os.path.join(dateDir, now)
54     print("时间目录:%s" % timeDir)
55     if not os.path.exists(timeDir):
56         # 如果以今天日期时间命名的目录不存在则创建
57         os.mkdir(timeDir)
58 
59     return timeDir
60 
61 
62 if __name__ == "__main__":
63     print(createLogDir())
64     print(createReportDir())

运行后结果截图:

 

标签:python,timeDir,存放,path,日志,now,os,目录,dateDir
来源: https://www.cnblogs.com/a-wyw/p/16223933.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有