一个简单的txt、log文本文件切割的脚本
- 说明
- 支持绝对路径和相对路径
- 在文件所在目录下会创建一个
cut_files
文件夹,用来存放切割好的文本文件 - 文件切分是以行数进行切分
- 参数说明
filename
:文件的绝对路径或者相对路径cut_pattern
:以固定行数进行切分
- 库说明
- 入口说明
from tqdm import trange
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)
platform = sys.platform
if platform == "win32":
file = current_path.split("\\")[-1]
else:
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=50000)