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

首頁 > 編程 > Python > 正文

Python腳本按照當前日期創(chuàng)建多級目錄

2020-02-16 01:26:29
字體:
供稿:網(wǎng)友

使用python腳本按照年月日生成多級目錄,創(chuàng)建的目錄可以將系統(tǒng)生成的日志文件放入其中,方便查閱,代碼如下:

#!/usr/bin/env python#coding=utf-8import timeimport os.path#獲得當前系統(tǒng)時間的字符串localtime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))print('localtime='+localtime)#系統(tǒng)當前時間年份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)#創(chuàng)建一個文件,以‘timeFile_'+具體時間為文件名稱fileDir=fileDay+'/timeFile_'+mdhms+'.txt'out=open(fileDir,'w')#在該文件中寫入當前系統(tǒng)時間字符串out.write('localtime='+localtime)out.close()

執(zhí)行

[root@localhost AccountInspector]# python timeFile.py localtime=2017-01-22 10:20:52

進入文件夾下,可以看到文件目錄已經(jīng)存在了

[root@localhost 22]# pwd/data/python-scripts/inspector/AccountInspector/badJsidAccountLogs/2017/01/22

文件也已經(jīng)生成

[root@localhost 22]# lltotal 4-rw-r--r--. 1 root root 29 Jan 22 10:20 timeFile_0122102052.txt

文件內(nèi)容

localtime=2017-01-22 10:20:52

總結

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對武林站長站的支持。如果你想了解更多相關內(nèi)容請查看下面相關鏈接

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石林| 晋江市| 成安县| 綦江县| 和静县| 平和县| 民乐县| 洪雅县| 清丰县| 内江市| 阿图什市| 和田县| 宁波市| 珠海市| 泗洪县| 毕节市| 锦州市| 沈阳市| 通江县| 商都县| 临泽县| 托里县| 海阳市| 福安市| 莆田市| 岚皋县| 杨浦区| 满洲里市| 双柏县| 信阳市| 湄潭县| 龙江县| 射阳县| 资兴市| 汉阴县| 鹿泉市| 南和县| 噶尔县| 利川市| 佛冈县| 台山市|