1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| import os import time import tkinter as tk from tkinter import filedialog from tkinter.messagebox import showinfo, showwarning, showerror def mkSubFile(lines, head, srcName, sub): [des_filename, extname] = os.path.splitext(srcName) filename = des_filename + '_' + str(sub) + extname print('make file: %s' % filename) fout = open(filename, 'w') try: fout.writelines([head]) fout.writelines(lines) return sub + 1 finally: fout.close() def splitByLineCount(filename, count): fin = open(filename, 'r') try: head = fin.readline() buf = [] sub = 1 for line in fin: buf.append(line) if len(buf) == count: sub = mkSubFile(buf, head, filename, sub) buf = [] if len(buf) != 0: sub = mkSubFile(buf, head, filename, sub) finally: fin.close() def init(): entryNum['state'] = "disable" btnConfirm['state'] = "normal" def confirm(): f_path = filedialog.askopenfilename() inputFilePath.set(f_path) def clear(): inputFilePath.set("") row.set("") def startSplitFile(): if len(inputFilePath.get()) == 0: showwarning(title="警告", message="未选择文件路径!") return try: if int(row.get()) <= 0: showwarning(title="警告", message="输入的不是正整数!") return except: showwarning(title="警告", message="输入的不是整数!") return count = int(row.get()) begin = time.time() splitByLineCount(inputFilePath.get(), count) end = time.time() print('time is %d seconds ' % (end - begin)) def closeWindow(): root.destroy() if __name__ == '__main__': root = tk.Tk() root.title("File Split") root.resizable(False, False) root.geometry("600x100+480+320") mess = tk.Label(root, text="请选择要切分的文件:") mess.place(x=20, y=10, width=200, height=20) inputFilePath = tk.StringVar(root, value='') entryNum = tk.Entry(root, width=80, textvariable=inputFilePath) entryNum.place(x=220, y=10, width=260, height=20) btnConfirm = tk.Button(root, text='选择文件', command=confirm) btnConfirm.place(x=500, y=10, width=70, height=20) mess1 = tk.Label(root, text="请输入切分的文件行数:") mess1.place(x=20, y=40, width=200, height=20) row = tk.StringVar(root, value='') entryNum1 = tk.Entry(root, width=80, textvariable=row) entryNum1.place(x=220, y=40, width=200, height=20) btnStart = tk.Button(root, text='清空', command=clear) btnStart.place(x=260, y=70, width=70, height=20) btnSet = tk.Button(root, text='开始切分', command=startSplitFile) btnSet.place(x=125, y=70, width=70, height=20) init() root.protocol("WM_DELETE_WINDOW", closeWindow) root.mainloop()
|