本文来源吾爱破解论坛
python多进程实现复制文件
https://www.52pojie.cn/thread-1041814-1-1.html
(出处: 吾爱破解论坛)
这是上一次的梯子,属于黑框操作,今天添加界面,实现简单的可视化操作。
程序附上:
[Python] 纯文本查看 复制代码
#界面 root = tk.Tk() root.title("复制文件") root.wm_geometry('400x500+150+100') root.wm_resizable(False,False) #操作按键 copy_button = tk.Button(root, text="复制" ,command=lambda :copy_main(queue)) copy_button.place(x=20, y=20, width= 60, height= 20) exit_button = tk.Button(root, text='退出', activebackground="red", command=root.quit) exit_button.place(x=320, y=20, width= 60, height= 20) #原文件位置 from_button = tk.Button(root, text='初始路径', command= from_file) from_button.place(x= 20, y= 60 , width= 60, height= 20 ) from_entry = tk.Entry(root) from_entry.place(x= 100, y= 60 , width = 280, height = 20) #目标文件位置 to_button = tk.Button(root, text='最终路径', command= to_file) to_button.place(x=20, y=100, width = 60, height= 20) to_entry = tk.Entry(root) to_entry.place(x=100,y=100, width= 280, height=20 ) #显示框 show_label = tk.Label(root, text="完成结果") show_label.place(x= 20, y=140, width= 60, height = 20) show_win = tk.Listbox(root, relief= 'groove') show_win.place(x=20, y =160, width = 360, height = 300) root.mainloop()
界面就是这样。
W09EM$}IZ[12)D`F(3X7D.png (14.37 KB, 下载次数: 0)
下载附件 保存到相册
至于功能,完完全全就是上次的帖子稍加修改,这里就不再赘述,直接附上程序。
[Python] 纯文本查看 复制代码
import tkinter as tk from tkinter import filedialog import multiprocessing import os import time def from_file(): #selectFile = tk.filedialog.askopenfilename() # askopenfilename 1次上传1个;askopenfilenames1次上传多个 selectFile = filedialog.askdirectory() from_entry.insert(0, selectFile) def to_file(): #selectFile = tk.filedialog.askopenfilename() # askopenfilename 1次上传1个;askopenfilenames1次上传多个 selectFile = filedialog.askdirectory() to_entry.insert(0, selectFile) #1得到复制的文件夹名 def get_old_folder_name(): old_folder_name = from_entry.get() return old_folder_name #2得到文件夹下的文件名 def get_file_names( folder ): file_names = os.listdir(folder) return file_names #3新建文件夹 def creat_folder(new_folder_name): try: os.mkdir(new_folder_name) except: pass return new_folder_name #4复制文件到指定文件夹下 def copy_file(file_name, old_folder_name, new_folder_name,queue): try: #如果是文件,进行复制 if os.path.isfile( old_folder_name+"/"+file_name): #不知道文件类型,直接使用二进制读取。 old_f = open(old_folder_name+"/"+file_name, "rb") read_str = old_f.read() old_f.close() new_f = open(new_folder_name +"/" + file_name, "wb") new_f.write(read_str) new_f.close() #如果是文件夹,进行递归。 新建文件夹--》获取子文件夹文件名--》复制文件 if os.path.isdir(old_folder_name+"/"+file_name): new_folder_name = creat_folder(new_folder_name+"/"+file_name) old_folder_name = old_folder_name +"/"+file_name file_names = get_file_names(old_folder_name) for file_name in file_names : copy_file(file_name, old_folder_name, new_folder_name, queue) except: pass #避免”复制“文件夹而产生的文件数量不够,程序不能结束 queue.put(1) #time.sleep(1)没有实际作用,只是为了看进度条的效果 time.sleep(1) #5主函数 def copy_main(queue): old_folder_name = get_old_folder_name() #print("原文件夹: ",old_folder_name) show_win.insert(len(show_win.get(0)),"原文件夹: "+old_folder_name) file_names = get_file_names(old_folder_name) #print("文件数量: ",len(file_names)) show_win.insert(len(show_win.get(0)),"文件数量: "+str( len(file_names) )) new_folder_name = to_entry.get() new_folder_name = creat_folder( new_folder_name) #print("新文件夹: ",new_folder_name) show_win.insert(len(show_win.get(0)),"新文件夹: "+new_folder_name) for item in file_names : pool.apply_async(copy_file, (item,old_folder_name,new_folder_name ,queue)) pool.close() file_num = 0 file_sum = len(file_names) while True: if not queue.empty(): queue.get() file_num+=1 #print("\r",end="") #print("▓"*int(file_num/file_sum *50), end="") #print(" 已完成 %.2f%%" % (file_num/file_sum*100), end="") show_win.insert( 3, str( '▓'* int(file_num/file_sum *50) ) ) show_win.insert( 4,"已完成: {}%".format(file_num/file_sum*100)) if file_num == file_sum: print() break #print("复制完成!") show_win.insert(len(show_win.get(0)),"复制成功!") if __name__ == '__main__': #功能 pool = multiprocessing.Pool(10) queue = multiprocessing.Manager().Queue() #界面 root = tk.Tk() root.title("复制文件") root.wm_geometry('400x500+150+100') root.wm_resizable(False,False) #操作按键 copy_button = tk.Button(root, text="复制" ,command=lambda :copy_main(queue)) copy_button.place(x=20, y=20, width= 60, height= 20) exit_button = tk.Button(root, text='退出', activebackground="red", command=root.quit) exit_button.place(x=320, y=20, width= 60, height= 20) #原文件位置 from_button = tk.Button(root, text='初始路径', command= from_file) from_button.place(x= 20, y= 60 , width= 60, height= 20 ) from_entry = tk.Entry(root) from_entry.place(x= 100, y= 60 , width = 280, height = 20) #目标文件位置 to_button = tk.Button(root, text='最终路径', command= to_file) to_button.place(x=20, y=100, width = 60, height= 20) to_entry = tk.Entry(root) to_entry.place(x=100,y=100, width= 280, height=20 ) #显示框 show_label = tk.Label(root, text="完成结果") show_label.place(x= 20, y=140, width= 60, height = 20) show_win = tk.Listbox(root, relief= 'groove') show_win.place(x=20, y =160, width = 360, height = 300) root.mainloop()
版权声明:
本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。