整理桌面源碼
本帖最后由 abc084568 于 2023-7-29 14:45 编辑https://drive.google.com/file/d/ ... IZ/view?usp=sharing
主要就是選擇目錄 然後在哪裡創資料夾把檔案丟進去分類
import os
import shutil
import tkinter as tk
from tkinter import filedialog
import pathlib
# 將指定位置的檔案整理到目標資料夾
def organize_files(source_path, target_path):
file_type_dict = {}
for filename in os.listdir(source_path):
if os.path.isfile(os.path.join(source_path, filename)):
file_extension = pathlib.Path(filename).suffix
file_type = file_extension
file_type_name = file_type_dict.get(file_type, file_type)
target_folder = os.path.join(target_path, file_type_name)
os.makedirs(target_folder, exist_ok=True)
shutil.move(os.path.join(source_path, filename), os.path.join(target_folder, filename))
# 選擇整理位置的檔案夾
def select_source_folder():
source_folder = filedialog.askdirectory()
if source_folder:
source_var.set(source_folder)
# 選擇整理資料夾創建位置
def select_target_folder():
target_folder = filedialog.askdirectory()
if target_folder:
target_var.set(target_folder)
# 開始整理檔案
def organize():
source_path = source_var.get()
target_path = target_var.get()
if source_path and target_path:
organize_files(source_path, target_path)
result_label.config(text="整理完成!")
else:
result_label.config(text="請選擇整理位置和目標位置。")
# 取消整理
def cancel_organize():
source_var.set("")
target_var.set("")
result_label.config(text="已取消整理。")
# 主程式
if __name__ == "__main__":
root = tk.Tk()
root.title("檔案整理程式")
root.geometry("400x300")# 調整高度為 300
source_label = tk.Label(root, text="選擇整理位置的檔案夾:")
source_label.pack(pady=10)
source_var = tk.StringVar()
source_entry = tk.Entry(root, textvariable=source_var, width=40)
source_entry.pack(pady=5)
source_button = tk.Button(root, text="瀏覽", command=select_source_folder)
source_button.pack(pady=5)
target_label = tk.Label(root, text="選擇整理資料夾創建位置:")
target_label.pack(pady=10)
target_var = tk.StringVar()
target_entry = tk.Entry(root, textvariable=target_var, width=40)
target_entry.pack(pady=5)
target_button = tk.Button(root, text="瀏覽", command=select_target_folder)
target_button.pack(pady=5)
organize_button = tk.Button(root, text="確定整理", command=organize)
organize_button.pack(pady=10)
cancel_button = tk.Button(root, text="取消整理", command=cancel_organize)
cancel_button.pack()
result_label = tk.Label(root, text="")
result_label.pack(pady=10)
root.mainloop()
页:
[1]