1 #!/usr/bin/python 2 # _*_coding:utf-8_*_ 3 import os 4 import datetime 5 6 def getDirName(): 7 # 年-月-日 时:分:秒 8 now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") 9 # 年 10 year_time =datetime.datetime.now().strftime('%Y') 11 # 年-月 12 month_time = datetime.datetime.now().strftime('%Y-%m') 13 # 年-月-日 14 daytime = datetime.datetime.now().strftime('%Y-%m-%d') 15 # 时:分:秒 16 hourtime = datetime.datetime.now().strftime("%H:%M:%S") 17 print(now_time + "\n"+daytime + "\n" + hourtime) 18 19 #得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 20 pwd = os.getcwd()+"\\"+year_time+"\\"+month_time+"\\"+daytime 21 # print(pwd) 22 # 文件路径 23 word_name = os.path.exists(pwd) 24 # 判断文件是否存在:不存在创建 25 if not word_name: 26 os.makedirs(pwd) 27 result={"status":1,"msg":"文件目录不存在,已自动生成!"} 28 else: 29 result = {"status": 0, "msg": "文件已存在!"} 30 return result 31 if __name__ == '__main__': 32 tt=getDirName() 33 print(tt)
需求点:
每天有24小时,5分钟粒度有24*12=288个
每个5分钟粒度的日志文件存储对应5分钟粒度数据
1 # -*- coding: utf-8 -*- 2 from apptest.day2 import getCinfigIni 3 import os 4 import random 5 import datetime 6 import configparser 7 def getDirName(): 8 '''获得年月日中的文件路径''' 9 years = getCinfigIni(key='date', value='year') 10 months = getCinfigIni(key='date', value='month') 11 days = getCinfigIni(key='date', value='day') 12 #得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 13 pwd = os.getcwd()+"\\"+years+"\\"+months+"\\"+days 14 word_name = os.path.exists(pwd) 15 if not word_name: 16 os.makedirs(pwd) 17 result = {"status": 1, "msg": "文件目录不存在,已自动生成!"} 18 print(result) 19 else: 20 result = {"status": 0, "msg": "文件已存在!"} 21 print(result) 22 return pwd 23 def getConfigDir(): 24 '''获取配置文件地址''' 25 pwd=os.getcwd()+"\\"+"content\\config.ini" 26 return pwd 27 28 def getCinfigIni(key,value): 29 '''读取配置文件内容''' 30 conn=configparser.ConfigParser() 31 32 conn.read(getConfigDir(), encoding="utf-8-sig") # 读取中文的话,此处是utf-8-sig,而不是utf-8 33 #conn.getint()可以返回int类型的数据 34 return conn.get(key,value) 35 36 def hms(): 37 '''生成数据''' 38 hour=[i for i in range(0,24)] 39 muite_value=[j for j in range(0,60,5)] 40 dataNum = getCinfigIni(key='data', value='dataNum') 41 number = 1 42 for item in hour: 43 for t in muite_value: 44 num = 1 45 46 while num<int(dataNum): 47 years = getCinfigIni(key='date', value='year') 48 months = getCinfigIni(key='date', value='month') 49 days = getCinfigIni(key='date', value='day') 50 date_time = datetime.date(int(years), int(months), int(days)) 51 ymd = date_time.strftime("%Y%m%d") 52 sfm = datetime.time(item, t, 00) 53 sfm2 = sfm.strftime("%H%M%S") 54 date_time2 = ymd + sfm2 55 domain = getCinfigIni(key='data', value='domain') 56 reqcode = getCinfigIni(key='data', value='reqcode') 57 # str字符串转换成list,可以使用split分割成列表 58 a = reqcode.split(',') 59 # random.choice可以获取随机的数据 60 code = random.choice(a) 61 # 获取flow数据 62 flowMax = getCinfigIni(key='data', value='flowMax') 63 flow_random = random.uniform(100, int(flowMax)) 64 city_data = getCinfigIni(key='data', value='city') 65 b = city_data.split(',') 66 city = random.choice(b) 67 file_name = os.path.join(getDirName(), "CN_test_" + date_time2 + "_1.log") 68 dates2 = date_time2 + "|" + domain + "|" + "118.0.0.96" + "|" + city + "|" + str( 69 flow_random) + "|" + "2" + "|" + "1" + "|"+code 70 71 num +=1 72 73 with open(file_name, 'a', encoding='UTF-8') as f: 74 f.write(dates2 + '\n') 75 print("写入完成!") 76 number += 1 77 print(number) 78 print("全部写入完成!") 79 80 if __name__ == '__main__': 81 hms()
配置文件config.ini
[data] dataNum=20 domain=test.lyl2.com dirPath= HW_nodeid=1255150202,1255150203 reqcode=200,205,206,302,303,400,404,405,500,502,503 city=551,889,315,991,548,987,336,774 reqMaxNum=1000 flowMax=4000 [date] year=2020 month=03 day=06 dataflow=1