国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python腳本按照當前日期創建多級目錄

2020-01-04 13:33:39
字體:
來源:轉載
供稿:網友

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

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到python教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝丰县| 桐梓县| 赤水市| 蓬溪县| 黄大仙区| 桐梓县| 剑川县| 青州市| 宜春市| 商南县| 吉木乃县| 闽清县| 顺昌县| 全南县| 米林县| 彰武县| 安徽省| 延安市| 巴青县| 周宁县| 威宁| 平阳县| 井研县| 甘南县| 三原县| 马公市| 虞城县| 舞钢市| 鹿泉市| 水城县| 古浪县| 周口市| 汝州市| 石阡县| 武宣县| 平昌县| 云和县| 都江堰市| 邵阳县| 民县| 怀来县|