python如何实现日期自动增加(Python脚本按照当前日期创建多级目录)
类别:脚本大全 浏览量:698
时间:2022-01-22 01:58:01 python如何实现日期自动增加
Python脚本按照当前日期创建多级目录使用python脚本按照年月日生成多级目录,创建的目录可以将系统生成的日志文件放入其中,方便查阅,代码如下:
|
#!/usr/bin/env python #coding=utf-8 import time import 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/' + 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() |
执行
|
[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] # ll total 4 -rw-r--r--. 1 root root 29 Jan 22 10:20 timeFile_0122102052.txt |
文件内容
localtime=2017-01-22 10:20:52
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对开心学习网的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/u012062455/article/details/54667060
您可能感兴趣
- python数据结构全攻略(详解python数据结构和算法)
- 怎么查看python的安装路径(查看python安装路径及pip安装的包列表及路径)
- python函数使用方法高级用法(Python函数的参数常见分类与用法实例详解)
- python获取日期(Python根据当前日期取去年同星期日期)
- python字典的key(Python使用dict.fromkeys快速生成一个字典示例)
- python画折线图(python使用Plotly绘图工具绘制水平条形图)
- python字符串匹配教程(Python字符串匹配之6种方法的使用详解)
- python 数据库实现学生管理系统(python+mysql实现教务管理系统)
- python爬虫书籍经典(Python利用lxml模块爬取豆瓣读书排行榜的方法与分析)
- pythontkinter循环显示文本(Python实现定时自动关闭的tkinter窗口方法)
- python实现在同一电脑上tcp通信(Python两台电脑实现TCP通信的方法示例)
- 怎么用python做随机矩阵(python实现杨氏矩阵查找)
- python下的sql处理(python中aioysql异步操作MySQL的方法)
- python模块都是自己的语法(详解Python_shutil模块)
- python实现linux服务(Python实现Linux监控的方法)
- python分步式进程计算(python中如何使用分步式进程计算详解)
- 朋友圈屏蔽你的人,可以直接看淡了(朋友圈屏蔽你的人)
- 金球奖只青睐那些会戴珠宝的女人(金球奖只青睐那些会戴珠宝的女人)
- 浙江省一个县,人口超40万,建县历史超1100年(浙江省一个县人口超40万)
- 五代十国南唐历代国君(五代十国南唐历代国君)
- 飞机引进工程师杨隆 匠人匠心,只争朝夕(飞机引进工程师杨隆)
- 三人行,她们是育人路上的 铁三角 团队(她们是育人路上的)
热门推荐
- oracle for update nowait和for update区别
- jenkins 设置gitlab(jenkins+gitlab+nginx部署前端应用实现)
- sqlserver2008手动备份方法(MSSQL 2008 自动备份数据库的设置方法)
- laravel新增数据表(laravel实现上传图片的两种方式小结)
- python弹球游戏编写过程(python实现坦克大战游戏 附详细注释)
- 弹性云主机的组成(什么叫弹性云服务器?弹性云主机的“弹性”反映在哪?)
- 关于虚拟机virtual box(vagrant+virtualBox构建虚拟机的方法)
- python怎么导入函数pandas(python将pandas datarame保存为txt文件的实例)
- 排序算法口诀php(PHP快速排序算法实现的原理及代码详解)
- 如何提高网站的用户粘度
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9