代码拉取完成,页面将自动刷新
# -*- coding:utf-8 -*-
"""
@version: python3.10
@author:谢坤
@contact: 1341409330@qq.com
@software: PyCharm
@file: log_write.py
@time: 2023/7/17
@description: 自动化测试/抓包日志书写模块
"""
import datetime
import json
import os
import glob
# class FileWriting:
# def __int__(self, status):
# """
# :param status: 写入文件的数据
# """
# self.status = status
# file_path = './log/packet_'
def get_filename(file_path):
_time = datetime.datetime.now().strftime(f"%Y%m%d")
filename = file_path + _time + '.txt'
if os.path.exists(filename) == 0 or os.path.getsize(filename) / 1024 < 10240:
filename = filename
# elif os.path.exists(f'./log/{_time}_.txt')
elif glob.glob(f'{file_path}{_time}_???.txt'):
filess = glob.glob(f'{file_path}{_time}_???.txt')
max_mtime = 0
# print(os.walk('./log'))
for dirname, subdirs, files in os.walk(os.path.dirname(filename)):
for fname in filess:
# 修复固定文件路径得问题,根据文件名设置下一个日志文件名称
if fname != file_path.split('/')[-1]+_time+'.txt':
full_path = os.path.join(dirname, fname)
mtime = os.stat(full_path).st_mtime
if mtime > max_mtime:
max_mtime = mtime
max_dir = dirname
max_file = fname
nums = int(max_file[-7:-4])+1
nums = str(nums).zfill(3)
print(max_dir, max_file, nums)
newfilename = file_path+_time+'_'+nums+'.txt'
try:
os.rename(filename, newfilename)
except Exception as e:
print(e)
else:
nums = "001"
newfilename = file_path +_time+'_'+nums+'.txt'
try:
os.rename(filename, newfilename)
except Exception as e:
print(e)
return filename
# json格式保存
def write_file(status, file_path=None):
if file_path is None:
file_path = './log/packet_'
# print(filename)
if not os.path.isdir(os.path.dirname(file_path)):
os.mkdir(os.path.dirname(file_path))
with open(get_filename(file_path), 'a', encoding='utf-8') as f:
f.write(json.dumps(status, indent=4, ensure_ascii=False))
f.write('\n')
f.close()
# 字符串格式保存
def write_file_raw(status, file_path=None):
"""
:param status: 日志数据
:param file_path: 日志路径
:return:
"""
if file_path is None:
file_path = './log/packet_'
# file_path = './log/oprcp_current_oracle_success_'
# print(filename)
if not os.path.isdir(os.path.dirname(file_path)):
os.mkdir(os.path.dirname(file_path))
with open(get_filename(file_path), 'a', encoding='utf-8') as f:
f.write(str(status))
f.write('\n')
f.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。