import os import tkinter as tk import numpy as np from tkinter import filedialog as tkFileDialog from tkinter import messagebox import shutil def start(): window = tk.Tk() # 创建窗口对象的背景色 window.title('复制文件程序') # 设置窗口的标题 window.geometry('400x250') # 设置窗口的大小 title = tk.Label(window, text='复制文件程序', font=('微软雅黑', 12), width=40, height=3) title.place(x=0, y=0, anchor='nw') choosepath = tk.Label(window, text='选择源文件夹:', font=('微软雅黑', 12), width=20, height=2) choosepath.place(x=0, y=50, anchor='nw') path_var1 = tk.StringVar() # 输入路径 entry1 = tk.Entry(window, textvariable=path_var1) entry1.place(x=170, y=65, anchor='nw') txtpath = tk.Label(window, text='选择txt:', font=('微软雅黑', 12), width=20, height=2) txtpath.place(x=0, y=100, anchor='nw') path_var2 = tk.StringVar() # 输入路径 entry2 = tk.Entry(window, textvariable=path_var2) entry2.place(x=170, y=115, anchor='nw') resultpath = tk.Label(window, text='输出文件夹:', font=('微软雅黑', 12), width=20, height=2) resultpath.place(x=0, y=150, anchor='nw') path_var6 = tk.StringVar() # 输出路径 entry6 = tk.Entry(window, textvariable=path_var6) entry6.place(x=170, y=165, anchor='nw') def choose2(): file_dir2 = tkFileDialog.askopenfilename(filetypes=[('TEXT', '*.txt'),('All Files', '*')]) path_var2.set(file_dir2) def choose1(): file_dir1 = tkFileDialog.askdirectory() path_var1.set(file_dir1) def choose3(): file_dir6 = tkFileDialog.askdirectory() path_var6.set(file_dir6) def tuichu(): window.destroy() def main(): inpath = entry1.get() txtpath = entry2.get() outpath = entry6.get() #读取txt with open(txtpath,'r') as filer: for ln in filer: str1 = ln.replace('\n','') #完整路径 apath = inpath + '/' + str1 #验证下路径是否存在 if (os.path.exists(apath) == False): print(apath + ' not exist!') else: opath = outpath + '/' + str1 #验证 if (os.path.exists(opath) == True): print(opath + ' exist!') opath = outpath + '/' + str1 + '_CT' #复制 shutil.copytree(apath,opath) messagebox.showinfo("消息","运行成功") window.destroy() tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw') tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw') tk.Button(window, text='选择', command=choose3).place(x=320, y=160, anchor='nw') tk.Button(window,text='确认',command=main).place(x=120,y=210,anchor='nw') tk.Button(window,text='取消',command=tuichu).place(x=220,y=210,anchor='nw') window.mainloop() if __name__ == '__main__': start()