Python脚本按照当前日期创建多级目录
Python  /  管理员 发布于 6年前   128
使用python脚本按照年月日生成多级目录,创建的目录可以将系统生成的日志文件放入其中,方便查阅,代码如下:
#!/usr/bin/env python#coding=utf-8import timeimport os.path#获得当前系统时间的字符串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='/data/python-scripts/inspector/AccountInspector/badJsidAccountLogs/'+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)#创建一个文件,以‘timeFile_'+具体时间为文件名称fileDir=fileDay+'/timeFile_'+mdhms+'.txt'out=open(fileDir,'w')#在该文件中写入当前系统时间字符串out.write('localtime='+localtime)out.close()
执行
[root@localhost AccountInspector]# python timeFile.py localtime=2017-01-22 10:20:52
进入文件夹下,可以看到文件目录已经存在了
[root@localhost 22]# pwd/data/python-scripts/inspector/AccountInspector/badJsidAccountLogs/2017/01/22
文件也已经生成
[root@localhost 22]# lltotal 4-rw-r--r--. 1 root root 29 Jan 22 10:20 timeFile_0122102052.txt
文件内容
localtime=2017-01-22 10:20:52
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家的支持。如果你想了解更多相关内容请查看下面相关链接
123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..路人 在
php中使用hyperf框架调用讯飞星火大模型实现国内版chatgpt功能示例中评论 教程很详细,如果加个前端chatgpt对话页面就完美了..Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号