1:需要知识点
python基础 file文件的操作
tkinter 桌面控制工具
#需要使用python基础来进行操作
from tkinter import filedialog
import tkinter as tk
import os
#创建一个类 调用类的初始化方法
class FileSplitterApp:
#初始化实现页面布局
def __init__(self,root):
self.root=root
self.root.title('文件切割器')
#创建标签
self.label1=tk.Label(root,text="选择的文件夹")
#将内容摆放
self.label1.pack(pady=10)
#创建按钮 # command方法不要加() 加了就变成了实例 直接点击了
self.button1=tk.Button(root,text="选择文件夹",command=self.select_file)
self.button1.pack(pady=10)
#切割文件按钮 调用切割的方法
self.button2 = tk.Button(root, text="切割文件",command=self.splite_file)
self.button2.pack(pady=10)
def select_file(self):
# 通过filedialog的方法 获取到选择的文件
file_path=filedialog.askopenfilename(filetypes=[("Text files", "*")])
#将地址返回给地址
self.file_path=file_path
#将内容设置给label1
self.label1.config(text=f'{file_path}')
pass
#切割文件
def splite_file(self):
#判断有没有这个元素 如果有则进行读取切割
if hasattr(self,"file_path"):
input_path=self.file_path
#打开文件 读取文件到一个变量
with open(input_path,'r',encoding="utf-8") as file1:
concate=file1.read()
#得到10分之1文件的大小而 // 是整数除法运算符,它返回不大于结果的最小整数
concate_size=len(concate)//10
# 获取文件的1/10 然后写入到第一个文件
for i in range(10):
start = i*concate_size
end = (i+1)*concate_size
newconcat=concate[start:end]
# 写一个新的文件名
#outputfile_path = f"{input_path.split('.')[0]}part{i}.{input_path.split('.')[1]}"
# splitetext 切割文件名
outputfile_path = f"{os.path.splitext(input_path)[0]}part{i}.{input_path.split('.')[1]}"
print(outputfile_path)
with open(outputfile_path, 'w', encoding="utf-8") as file2:
file2.write(newconcat)
tk.messagebox.showinfo("完成", "文件切割完成!")
#没有则弹出提示框
else:
tk.messagebox.showwarning("警告", "请先选择文件!")
pass
if __name__ == '__main__':
#创建一个窗口
root=tk.Tk()
#创建实例
app = FileSplitterApp(root)
#主结构循环
root.mainloop()