1 Star 0 Fork 0

旋枫/pyshark_xf

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
log_write.py 2.94 KB
一键复制 编辑 原始数据 按行查看 历史
旋枫 提交于 2023-08-02 06:49 . update log_write.py.
# -*- 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()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/KevinXF/pyshark_xf.git
git@gitee.com:KevinXF/pyshark_xf.git
KevinXF
pyshark_xf
pyshark_xf
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385