- import os
- import shutil
- import py7zr
- import datetime
- import tkinter as tk
- from tkinter import filedialog
- from tkinter import messagebox
- def split_folder(folder_path, max_size):
- files = os.listdir(folder_path)
- current_folder = None
- current_size = 0
- folder_index = 1
- for file_name in files:
- file_path = os.path.join(folder_path, file_name)
- if os.path.isfile(file_path):
- if current_folder is None or current_size + os.path.getsize(file_path) > max_size:
- current_folder = os.path.join(folder_path, f"folder_{folder_index}")
- os.mkdir(current_folder)
- folder_index += 1
- current_size = 0
- shutil.move(file_path, current_folder)
- current_size += os.path.getsize(os.path.join(current_folder, file_name))
- def encrypt_file(file_path, password):
- archive_path = file_path + ".7z"
- with py7zr.SevenZipFile(archive_path, 'w', password=password) as archive:
- archive.writeall(file_path, os.path.basename(file_path))
- os.remove(file_path)
- os.rename(archive_path, file_path)
- def decrypt_file(file_path, password):
- archive_path = file_path + ".7z"
- os.rename(file_path, archive_path)
- with py7zr.SevenZipFile(archive_path, 'r', password=password) as archive:
- archive.extractall(path=os.path.dirname(file_path))
- os.remove(archive_path)
- def add_password_hint(file_path, password_hint):
- new_file_name = f"{os.path.splitext(file_path)[0]} [{password_hint}]{os.path.splitext(file_path)[1]}"
- os.rename(file_path, new_file_name)
- def add_timestamp(file_path):
- timestamp = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
- new_file_name = f"{os.path.splitext(file_path)[0]} [{timestamp}]{os.path.splitext(file_path)[1]}"
- os.rename(file_path, new_file_name)
- def browse_folder():
- folder_path = filedialog.askdirectory()
- folder_path_entry.delete(0, tk.END)
- folder_path_entry.insert(tk.END, folder_path)
- def process_folder():
- folder_path = folder_path_entry.get()
- max_size = int(max_size_entry.get())
- password = password_entry.get()
- password_hint = password_hint_entry.get()
- split_folder(folder_path, max_size)
- for file_name in os.listdir(folder_path):
- file_path = os.path.join(folder_path, file_name)
- encrypt_file(file_path, password)
- add_password_hint(file_path, password_hint)
- add_timestamp(file_path)
- messagebox.showinfo("Done", "Processing complete!")
- window = tk.Tk()
- window.title("File Management Tool")
- folder_path_label = tk.Label(window, text="Folder Path:")
- folder_path_label.grid(row=0, column=0, padx=10, pady=5)
- folder_path_entry = tk.Entry(window, width=50)
- folder_path_entry.grid(row=0, column=1, padx=10, pady=5)
- browse_button = tk.Button(window, text="Browse", command=browse_folder)
- browse_button.grid(row=0, column=2, padx=10, pady=5)
- max_size_label = tk.Label(window, text="Max Size (bytes):")
- max_size_label.grid(row=1, column=0, padx=10, pady=5)
- max_size_entry = tk.Entry(window, width=20)
- max_size_entry.grid(row=1, column=1, padx=10, pady=5)
- password_label = tk.Label(window, text="Password:")
- password_label.grid(row=2, column=0, padx=10, pady=5)
- password_entry = tk.Entry(window, width=20)
- password_entry.grid(row=2, column=1, padx=10, pady=5)
- password_hint_label = tk.Label(window, text="Password Hint:")
- password_hint_label.grid(row=3, column=0, padx=10, pady=5)
- password_hint_entry = tk.Entry(window, width=20)
- password_hint_entry.grid(row=3, column=1, padx=10, pady=5)
- process_button = tk.Button(window, text="Process", command=process_folder)
- process_button.grid(row=4, column=0, columnspan=3, padx=10, pady=5)
- window.mainloop()
複製代碼 開源的,點用自便 |