Python实现按当前日期(年、月、日)创建多级目录的方法


本文摘自php中文网,作者不言,侵删。

这篇文章主要介绍了关于Python实现按当前日期(年、月、日)创建多级目录的方法 ,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

先看实际效果,现在时间2018.4.26

使用python脚本按照年月日生成多级目录,创建的目录可以将系统生成的日志文件放入其中,方便查阅,代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

#!/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()

阅读剩余部分

相关阅读 >>

Python queue模块

详解Python中for循环的工作原理

Python要用什么软件

Python中找出numpy array数组的最值及其索引方法

使用Python通过win32 com实现word文档的写入与保存方法

Python中matplotlib库的用法介绍

Python如何统计字符串中字母个数?

Python输代码怎么换行?

Python怎么查看变量类型

Python单链表中如何查找和删除节点?

更多相关阅读请进入《Python》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...