- import os
- import subprocess
- import tkinter as tk
- from tkinter import filedialog
- def split_file():
- # Get file path and target size
- file_path = file_path_entry.get()
- target_size_mb = int(target_size_entry.get())
- # Calculate the target file size (in bytes)
- target_size_bytes = target_size_mb * 1024 * 1024
- #Create output file directory
- output_dir = os.path.dirname(file_path) + '/split_files'
- os.makedirs(output_dir, exist_ok=True)
- # Split file command line command
- split_cmd = f'split -b {target_size_bytes} "{file_path}" "{output_dir}/part"'
- # Execute the split file command line command
- subprocess.run(split_cmd, shell=True)
- # Split completion prompt
- tk.messagebox.showinfo('Split file', 'The file has been successfully split into partial files!')
- def merge_files():
- # Get the merged file path and split file directory
- output_file_path = output_file_entry.get()
- input_dir = input_dir_entry.get()
- # Merge file command line instructions
- merge_cmd = f'cat "{input_dir}"/part* > "{output_file_path}"'
- # Execute merge file command line instructions
- subprocess.run(merge_cmd, shell=True)
- # Merge completion prompt
- tk.messagebox.showinfo('Merge files', 'Files have been merged successfully!')
- def browse_file():
- #Open the file dialog box and get the file path
- file_path = filedialog.askopenfilename()
- file_path_entry.delete(0, tk.END)
- file_path_entry.insert(0, file_path)
- def browse_output_file():
- # Open the file dialog box and get the merged file path
- output_file_path = filedialog.asksaveasfilename()
- output_file_entry.delete(0, tk.END)
- output_file_entry.insert(0, output_file_path)
- def browse_input_dir():
- # Open the directory dialog box and obtain the split file directory
- input_dir = filedialog.askdirectory()
- input_dir_entry.delete(0, tk.END)
- input_dir_entry.insert(0, input_dir)
- #Create the main window
- window = tk.Tk()
- window.title('File splitting and merging tool')
- #File selection box
- file_path_label = tk.Label(window, text='Select file:')
- file_path_label.pack()
- file_path_entry = tk.Entry(window, width=50)
- file_path_entry.pack()
- browse_file_button = tk.Button(window, text='browse', command=browse_file)
- browse_file_button.pack()
- # Target size box
- target_size_label = tk.Label(window, text='Target size (MB):')
- target_size_label.pack()
- target_size_entry = tk.Entry(window, width=10)
- target_size_entry.pack()
- # Split file button
- split_button = tk.Button(window, text='Split file', command=split_file)
- split_button.pack()
- # Merge file box
- output_file_label = tk.Label(window, text='Merge files:')
- output_file_label.pack()
- output_file_entry = tk.Entry(window, width=50)
- output_file_entry.pack()
- browse_output_file_button = tk.Button(window, text='browse', command=browse_output_file)
- browse_output_file_button.pack()
- # Split file directory box
- input_dir_label = tk.Label(window, text='Split file directory:')
- input_dir_label.pack()
- input_dir_entry = tk.Entry(window, width=50)
- input_dir_entry.pack()
- browse_input_dir_button = tk.Button(window, text='Browse', command=browse_input_dir)
- browse_input_dir_button.pack()
- # Merge file button
- merge_button = tk.Button(window, text='Merge files', command=merge_files)
- merge_button.pack()
複製代碼 開源的,要用自便 |