|
60鱼币
本帖最后由 yixinwenxin 于 2024-8-13 16:11 编辑
```
import os
import shutil
import filecmp
import threading
from tkinter import *
from tkinter import ttk
nas_path = r"\\10.168.2.1\vol1\code_note_python"
local_path = os.getcwd()
def local_sync_nas():
def sync(nas_path, local_path):
# 差集删除文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
del_files = nas_dir - local_dir
for number, del_file in enumerate(del_files, start=1):
del_nas = os.path.join(nas_path, del_file)
if os.path.isfile(del_nas):
os.remove(del_nas)
else:
shutil.rmtree(del_nas)
v = int(number / len(del_files) * 25) if len(del_files) > 0 else 25
update_progress(v)
percent_text.set(f"{v}%")
# 差集增加文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
add_files = local_dir - nas_dir
for number, add_file in enumerate(add_files, start=1):
add_nas = os.path.join(nas_path, add_file)
add_local = os.path.join(local_path, add_file)
if os.path.isfile(add_local):
shutil.copy2(add_local, add_nas)
else:
if not os.path.exists(add_nas):
os.mkdir(add_nas)
v = int(number / len(add_files) * 25) + 25 if len(add_files) > 0 else 50
update_progress(v)
percent_text.set(f"{v}%")
# 交集更新文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
comparison_files = nas_dir & local_dir
for number, comparison_file in enumerate(comparison_files, start=1):
nas_file = os.path.join(nas_path, comparison_file)
local_file = os.path.join(local_path, comparison_file)
if os.path.isdir(local_file):
sync(nas_file, local_file)
else:
if not filecmp.cmp(nas_file, local_file, shallow=True):
shutil.copy2(local_file, nas_file)
v = int(number / len(comparison_files) * 50) + 50 if len(comparison_files) > 0 else 100
update_progress(v)
percent_text.set(f"{v}%")
# 每个文件夹结束后都会来到这里判断,但我想要所有的文件都同步后再来判断。我不知道该怎么写了...求指教
if on_select():
close()
thread = threading.Thread(target=sync, args=(nas_path, local_path), daemon=True)
thread.start()
def nas_sync_local():
def sync(nas_path, local_path):
# 差集删除文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
del_files = local_dir - nas_dir
for number, del_file in enumerate(del_files, start=1):
del_local = os.path.join(local_path, del_file)
if os.path.isfile(del_local):
os.remove(del_local)
else:
shutil.rmtree(del_local)
v = int(number / len(del_files) * 25) if len(del_files) > 0 else 25
update_progress(v)
percent_text.set(f"{v}%")
# 差集增加文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
add_files = nas_dir - local_dir
for number, add_file in enumerate(add_files, start=1):
add_nas = os.path.join(nas_path, add_file)
add_local = os.path.join(local_path, add_file)
if os.path.isfile(add_nas):
shutil.copy2(add_nas, add_local)
else:
if not os.path.exists(add_local):
os.mkdir(add_local)
v = int(number / len(add_files) * 25) + 25 if len(add_files) > 0 else 50
update_progress(v)
percent_text.set(f"{v}%")
# 交集更新文件
nas_dir = set(os.listdir(nas_path))
local_dir = set(os.listdir(local_path))
comparison_files = nas_dir & local_dir
for number, comparison_file in enumerate(comparison_files, start=1):
nas_file = os.path.join(nas_path, comparison_file)
local_file = os.path.join(local_path, comparison_file)
if os.path.isdir(nas_file):
sync(nas_file, local_file)
else:
if not filecmp.cmp(nas_file, local_file, shallow=True):
shutil.copy2(nas_file, local_file)
v = int(number / len(comparison_files) * 50) + 50 if len(comparison_files) > 0 else 100
update_progress(v)
percent_text.set(f"{v}%")
# 每个文件夹结束后都会来到这里判断,但我想要所有的文件都同步后再来判断。我不知道该怎么写了...求指教
if on_select():
close()
thread = threading.Thread(target=sync, args=(nas_path, local_path), daemon=True)
thread.start()
def update_progress(value: int) -> None:
progress_bar["value"] = value
def on_select() -> bool:
return combo_box.get() == "True"
def close() -> None:
os.system("shutdown /s /t 10")
root = Tk()
root.title("python")
# 设置窗口尺寸和位置
window_width = "325"
window_height = "75"
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x_offset = screen_width // 2 - int(window_width) // 2
y_offset = screen_height // 2 - int(window_height) // 2
root.geometry(f"{window_width}x{window_height}+{x_offset}+{y_offset}")
root.resizable(False, False)
# 布局
top_frame = Frame(master=root)
top_frame.pack(expand=True)
Frame(height=2, bd=1, relief=SUNKEN).pack(fill=X, padx=1, pady=1)
bottom_frame = Frame(master=root)
bottom_frame.pack(expand=True)
# 进度条和百分比显示
percent_text = StringVar()
percent_text.set("0%")
progress_bar = ttk.Progressbar(master=top_frame, mode="determinate", length=290)
progress_bar.grid(row=1, column=1)
percent_label = Label(master=top_frame, textvariable=percent_text)
percent_label.grid(row=1, column=2)
# 完成后是否关机的选择
options = ["True", "False"]
Label(master=bottom_frame, text="完成后是否关机").grid(row=1, column=1)
combo_box = ttk.Combobox(master=bottom_frame, values=options, width=3)
combo_box.grid(row=1, column=2, padx=2)
combo_box.current(1)
# 按钮
Button(master=bottom_frame, text="[nas]->[local]", command=nas_sync_local).grid(row=1, column=3)
Button(master=bottom_frame, text="[nas]<-[local]", command=local_sync_nas).grid(row=1, column=4)
root.mainloop()
```问题 如下 # 每个文件夹结束后都会来到这里判断,但我想要所有的文件都同步后再来判断。我不知道该怎么写了...求指教
if on_select():
close()
|
|