Python实现按当前日期(年、月、日)创建多级目录的方法
作者:听风。 时间:2022-05-01 09:22:01
先看实际效果,现在时间2018.4.26
使用python脚本按照年月日生成多级目录,创建的目录可以将系统生成的日志文件放入其中,方便查阅,代码如下:
#!/usr/bin/env python
#coding=utf-8
import time
import os
#获得当前系统时间的字符串
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()))
#具体时间 小时分钟毫秒
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)
#创建一个文件,以‘timeFile_'+具体时间为文件名称
fileDir=fileDay+'/timeFile_'+mdhms+'.txt'
out=open(fileDir,'w')
#在该文件中写入当前系统时间字符串
out.write('localtime='+localtime)
out.close()
关于日期时间的其他知识点
import datetime
today = datetime.date.today()
想要指定到時分秒的話可以搞成這樣
import datetime
#這就是指定 2008/12/5 23:59:59
today = datetime.datetime(2008, 12, 5, 23, 59, 59)
#datetime 也可以這樣做加減,一次加一秒
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)
#一次加 23小時 59分 59秒
x = datetime.timedelta(hours = 23, minutes = 59, seconds = 59)
w = w + x
#w = datetime.datetime(2008, 12, 6, 23, 59, 59)
還有就是,如果想要拿到今天的年,月,日 也是很簡單的說
import datetime
x = datetime.datetime.now() #現在時間
#x = datetime.datetime(2008, 12, 5, 23, 59, 59) #指定時間
x.year #會拿到 2008
x.month #會拿到 12
x.day # 會拿到 5
x.hour #時
x.minute #分
x.second #秒 59
总结
以上所述是小编给大家介绍的Python实现按当前日期(年、月、日)创建多级目录的方法网站的支持!
来源:https://www.cnblogs.com/huchong/archive/2018/04/26/8951715.html
标签:Python,创建,目录
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
PHP get_html_translation_table()函数用法讲解
2023-06-05 18:36:00
![](https://img.aspxhome.com/file/2023/7/55387_0s.png)
Mootools 1.2教程(21)——类(二)
2008-12-28 20:58:00
pytorch geometric的GNN、GCN的节点分类方式
2022-12-24 16:01:23
如何判断用户是否非正常离开聊天室?
2010-01-18 20:30:00
php线性表顺序存储实现代码(增删查改)
2023-11-19 06:51:53
如何在python中使用selenium的示例
2023-07-15 20:52:09
pywinauto自动化操作记事本
2021-07-19 03:01:57
解决Python复杂zip文件的解压问题
2021-08-11 05:04:09
![](https://img.aspxhome.com/file/2023/9/75329_0s.jpg)
在ipython notebook中使用argparse方式
2021-11-17 08:58:41
Python学习之字典和集合的使用详解
2022-11-01 19:54:21
详解Python中pyautogui库的最全使用方法
2022-11-17 17:07:14
Python3中小括号()、中括号[]、花括号{}的区别详解
2022-05-31 09:20:36
javascript实现日期3级联动下拉框选择菜单
2023-10-19 02:15:34
python神经网络Batch Normalization底层原理详解
2021-01-28 12:50:08
![](https://img.aspxhome.com/file/2023/2/75292_0s.png)
Python生成器(Generator)详解
2022-10-05 01:14:43
Python对list列表结构中的值进行去重的方法总结
2021-10-18 04:09:04
NumPy索引与切片的用法示例总结
2022-05-13 19:33:38
![](https://img.aspxhome.com/file/2023/4/80544_0s.png)
Python进阶之协程详解
2023-08-23 04:52:52
![](https://img.aspxhome.com/file/2023/0/62870_0s.png)
Python中的“_args”和“__kwargs”用法详解
2023-03-11 02:10:38
python的列表List求均值和中位数实例
2023-07-09 13:06:16