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
您可能感兴趣
- pythonlambda详解(Python的条件表达式和lambda表达式实例)
- python实现购物网站(Python实战购物车项目的实现参考)
- python3函数的使用方法(Python3.5基础之函数的定义与使用实例详解参数、作用域、递归、重载等)
- python 多进程读取文件(Python实现的多进程拷贝文件并显示百分比功能示例)
- python的def命令参数(python ddt数据驱动最简实例代码)
- java入坑rabbitmq(Python操作rabbitMQ的示例代码)
- python教程列表排序(Python一行代码实现快速排序的方法)
- eclipse配置python(eclipse创建python项目步骤详解)
- python处理各种xml文件(Python使用sax模块解析XML文件示例)
- pythonssl版本(解决Python 使用h5py加载文件,看不到keys的问题)
- python有什么好的微信公众号(python下载微信公众号相关文章)
- python读取文件的方法和区别(浅谈PYTHON 关于文件的操作)
- python3有哪些内置模块(Python3.5内置模块之os模块、sys模块、shutil模块用法实例分析)
- python爬虫怎么爬取vip资源(Python网络爬虫之爬取微博热搜)
- python操作json格式(详解python 3.6 安装json 模块simplejson)
- python画图教程(详解用Python练习画个美队盾牌)
- 中国留学生都是富二代吗()
- 我们现在吃的苹果是哪里来的 原来现代苹果引入中国仅有一百多年(我们现在吃的苹果是哪里来的)
- 买绿宝不能只挑黄绿色 菜农教你3招挑,个个皮薄肉脆,香甜爆汁(买绿宝不能只挑黄绿色)
- 大果肉搭配薄瓜皮, 绿宝 脆甜爽口,不愧是甜瓜中的 佼佼者(大果肉搭配薄瓜皮)
- 河南尉氏县因地制宜发展果蔬种植 水坡镇绿宝甜瓜变 金瓜(河南尉氏县因地制宜发展果蔬种植)
- 谢广坤,你这么欺负谢腾飞,良心不会痛吗(你这么欺负谢腾飞)
热门推荐
- 运行docker时提示端口已被占用(docker 突然不能被外网访问的解决方案)
- 云服务器选择七个原则(如何选择一个好的云服务器?)
- vue实现一个炫酷的日历组件(vue利用Moment插件格式化时间的实例代码)
- django条件序列化(Django之无名分组和有名分组的实现)
- C#中ToDictionary,ToLookup
- 前端实现一个自适应的正方形(前端实现背景虚化但内容清晰且自适应 的实例代码)
- zabbix基本监控配置(zabbix使用教程+受监控服务器的添加安装教程)
- 网页颜色的选择
- mysql数据表实例教程(mysql数据库入门第一步之创建表)
- elasticsearch docker(在Docker中安装Elasticsearch7.6.2的教程)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9