Python脚本按照当前日期创建多级目录


Posted in Python onMarch 01, 2019

使用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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
Python转换HTML到Text纯文本的方法
Jan 15 Python
python将MongoDB里的ObjectId转换为时间戳的方法
Mar 13 Python
Python设计实现的计算器功能完整实例
Aug 18 Python
Python数据结构与算法之字典树实现方法示例
Dec 13 Python
Django中的CBV和FBV示例介绍
Feb 25 Python
Python读写docx文件的方法
May 08 Python
Python基础之函数的定义与使用示例
Mar 23 Python
python买卖股票的最佳时机(基于贪心/蛮力算法)
Jul 05 Python
python 6.7 编写printTable()函数表格打印(完整代码)
Mar 25 Python
Keras官方中文文档:性能评估Metrices详解
Jun 15 Python
Python下使用Trackbar实现绘图板
Oct 27 Python
详解Open Folder as PyCharm Project怎么添加的方法
Dec 29 Python
Python enumerate函数功能与用法示例
Mar 01 #Python
django模板加载静态文件的方法步骤
Mar 01 #Python
Python3中编码与解码之Unicode与bytes的讲解
Feb 28 #Python
Python multiprocessing多进程原理与应用示例
Feb 28 #Python
Python中的异常处理try/except/finally/raise用法分析
Feb 28 #Python
python使用phoenixdb操作hbase的方法示例
Feb 28 #Python
Python面向对象程序设计中类的定义、实例化、封装及私有变量/方法详解
Feb 28 #Python
You might like
脚本之家贴图转换+转贴工具用到的js代码超级推荐
2007/04/05 Javascript
锋利的jQuery 要点归纳(三) jQuery中的事件和动画(下:动画篇)
2010/03/24 Javascript
js面向对象设计用{}好还是function(){}好(构造函数)
2011/10/23 Javascript
Javascript 鼠标移动上去 滑块跟随效果代码分享
2013/11/23 Javascript
jquery实现html页面 div 假分页有原理有代码
2014/09/06 Javascript
JavaScript访问字符串中单个字符的两种方法
2015/07/03 Javascript
javascript 动态脚本添加的简单方法
2016/10/11 Javascript
ES6新特性一: let和const命令详解
2017/04/20 Javascript
文本溢出插件jquery.dotdotdot.js使用方法详解
2017/06/22 jQuery
React-router v4 路由配置方法小结
2017/08/08 Javascript
JS判断数组那点事
2017/10/10 Javascript
JavaScript中EventLoop介绍
2018/01/22 Javascript
JavaScript面向对象的程序设计(犯迷糊的小羊)
2018/05/27 Javascript
在vue项目中引入高德地图及其UI组件的方法
2018/09/04 Javascript
详解nuxt 微信公众号支付遇到的问题与解决
2019/08/26 Javascript
vuecli项目构建SSR服务端渲染的实现
2020/10/30 Javascript
python 2.6.6升级到python 2.7.x版本的方法
2016/10/09 Python
python中Switch/Case实现的示例代码
2017/11/09 Python
Django1.9 加载通过ImageField上传的图片方法
2018/05/25 Python
python 查找文件名包含指定字符串的方法
2018/06/05 Python
python3 面向对象__类的内置属性与方法的实例代码
2018/11/09 Python
详解python中@的用法
2019/03/27 Python
Python编写打字训练小程序
2019/09/26 Python
解决jupyter notebook打不开无反应 浏览器未启动的问题
2020/04/10 Python
python轮询机制控制led实例
2020/05/03 Python
Python实现电视里的5毛特效实例代码详解
2020/05/15 Python
Pycharm无法打开双击没反应的问题及解决方案
2020/08/17 Python
Django使用django-simple-captcha做验证码的实现示例
2021/01/07 Python
html5 postMessage解决跨域、跨窗口消息传递方案
2016/12/20 HTML / CSS
鼠标滚轮事件和Mac触控板双指事件
2019/12/23 HTML / CSS
波兰数码相机及配件网上商店: Cyfrowe.pl
2017/06/19 全球购物
Internet主要有哪些网络群组成
2015/12/24 面试题
酒店中秋节促销方案
2014/01/30 职场文书
关于读书的演讲稿600字
2014/08/27 职场文书
MySQL Router实现MySQL的读写分离的方法
2021/05/27 MySQL
使用vue判断当前环境是安卓还是IOS
2022/04/12 Vue.js