本帖最后由 klmatao 于 2021-10-27 20:32 编辑
[md]## 一个简单的txt、log文本文件切割的脚本
- 说明
- 支持绝对路径和相对路径
- 在文件所在目录下会创建一个
cut_files 文件夹,用来存放切割好的文本文件
- 文件切分是以行数进行切分
- 参数说明
filename :文件的绝对路径或者相对路径
cut_pattern :以固定行数进行切分
- 库说明
os :用来处理路径问题
tqdm :用来可视化进度
- 安装
tqdm : pip install tqdm
- 入口说明
import sys
from tqdm import trange
# 切分txt、或者log文件
# 首先有文件读取的方法
# 文件内容总行数
def total_lines(filename):
try:
with open(filename, "rb") as file:
contents = file.readlines()
return len(contents)
except Exception as e:
print(f"【{total_lines.__name__}】函数运行出错:{e}")
# 根据文件总行数,以及想要切割的行数,分批次获取文件,内容,然后进行写入操作
# 从文件中获取到所有的行,每一行是列表中的一个元素
def get_contents(filename):
try:
with open(filename, "rb") as file:
contents = file.readlines()
return contents
except Exception as e:
print(f"【{get_contents.__name__}】函数运行出错:{e}")
# 写入文件
def file_save(filename, contents):
try:
with open(filename, "wb") as file:
for content in contents:
file.write(content)
except Exception as e:
print(f"【{file_save.__name__}】函数运行出错:{e}")
# 分割文件
def cut_file_save_many_files(filename, cut_pattern):
if cut_pattern <= 0:
print("参数错误,必须大于0")
return
try:
# 获取总行数
total_line = total_lines(filename)
# 获取所有内容的列表
contents = get_contents(filename)
# 切分规则
counts = int(total_line / cut_pattern) + 1
# 新文件名的设置, 获取最后一个点的索引
change_filename = get_file_path(filename)
end_point_index = change_filename.rfind(".")
for i in trange(1, counts + 1):
new_filename = change_filename[:end_point_index] + \
f"_{i}" + change_filename[end_point_index:]
cut_content = contents[(i - 1) * cut_pattern:i * cut_pattern]
# 写入操作
file_save(new_filename, cut_content)
except Exception as e:
print(f"【{cut_file_save_many_files.__name__}】函数运行出错:{e}")
def get_file_path(filename):
import os
current_path = os.path.abspath(filename)
# 获取pc的系统平台
platform = sys.platform
if platform == "win32":
file = current_path.split("\\")[-1]
elif platform == "linux":
file = current_path.split("/")[-1]
parent_path = os.path.dirname(current_path)
path = os.path.join(parent_path, "cut_files")
if not os.path.exists(path):
os.mkdir(path)
return path + ("\\" if platform == "win32" else "/") + file
def start(filename, cut_pattern):
cut_file_save_many_files(filename, cut_pattern)
if __name__ == "__main__":
filename = "a.log"
start(filename, cut_pattern=300)
|