Advertisement
MizunoBrasil

Copia de múltiplos arquivos em Python 2.0

Aug 28th, 2024
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.72 KB | None | 0 0
  1. import os
  2. import threading
  3. import time
  4. from tkinter import *
  5. from tkinter.ttk import *
  6. from tkinter import filedialog, messagebox
  7.  
  8. def start_copy():
  9.     src_files = src_entry.get().split(";")  # Divide os arquivos selecionados
  10.     dst_folder = dst_entry.get()
  11.  
  12.     # Verifica se nenhum arquivo foi selecionado
  13.     if not src_files or src_files == ['']:
  14.         messagebox.showwarning("Aviso", "É necessário escolher um ou mais arquivos para copiar.")
  15.         return
  16.  
  17.     for src in src_files:
  18.         if os.path.isfile(src):
  19.             filename = os.path.basename(src)  # Obtém o nome do arquivo
  20.             dst = os.path.join(dst_folder, filename)  # Define o caminho de destino
  21.  
  22.             GB = os.path.getsize(src) / (1024 * 1024 * 1024)
  23.             copied = 0
  24.             chunk_size = 1024 * 1024  # 1MB
  25.            
  26.             with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst:
  27.                 while chunk := fsrc.read(chunk_size):
  28.                     fdst.write(chunk)
  29.                     copied += len(chunk) / (1024 * 1024 * 1024)
  30.                     progress = (copied / GB) * 100
  31.                     bar['value'] = progress
  32.                     percent.set(f"{int(progress)}%")
  33.                     text.set(f"{round(copied, 2)}/{round(GB, 2)} GB completos")
  34.                     window.update_idletasks()
  35.  
  36.     # Exibe o alerta ao final da cópia
  37.     messagebox.showinfo("Sucesso", "Cópia concluída com sucesso!")
  38.     os.startfile(dst_folder)  # Abre a pasta de destino
  39.  
  40. def select_src():
  41.     src_files = filedialog.askopenfilenames()  # Permite a seleção de múltiplos arquivos
  42.     src_entry.delete(0, END)
  43.     src_entry.insert(0, ";".join(src_files))  # Insere os arquivos selecionados, separados por ";"
  44.  
  45. def select_dst():
  46.     dst_folder = filedialog.askdirectory()  # Escolhe a pasta de destino
  47.     dst_entry.delete(0, END)
  48.     dst_entry.insert(0, dst_folder)
  49.  
  50. def start_thread():
  51.     threading.Thread(target=start_copy).start()
  52.  
  53. def show_about():    
  54.     messagebox.showinfo("Sobre", "Programa para cópia de múltiplos arquivos\n29/08/2024, Mizuno")
  55.  
  56. def exit_program():
  57.     window.quit()
  58.  
  59. # Configuração da janela
  60. window = Tk()
  61. window.title("GigaCopy 2.0")
  62. window.geometry('500x250')  # Mantém a altura da janela para 250 pixels e a altura em 500 pixels
  63. window.eval('tk::PlaceWindow . center')
  64.  
  65. # Menu superior
  66. menu_bar = Menu(window)
  67. file_menu = Menu(menu_bar, tearoff=0)
  68. file_menu.add_command(label="Sobre", command=show_about)
  69. file_menu.add_separator()
  70. file_menu.add_command(label="Sair", command=exit_program)
  71. menu_bar.add_cascade(label="Arquivo", menu=file_menu)
  72. window.config(menu=menu_bar)
  73.  
  74. # Variáveis para a barra de progresso
  75. percent = StringVar()
  76. text = StringVar()
  77.  
  78. # Layout em grid
  79. Label(window, text="Origem:").grid(row=0, column=0, padx=10, pady=10, sticky=E)
  80. src_entry = Entry(window, width=50)
  81. src_entry.grid(row=0, column=1, padx=10, pady=10)
  82. Button(window, text="...", command=select_src, width=3).grid(row=0, column=2, padx=10, pady=10)  # Largura reduzida
  83.  
  84. Label(window, text="Destino:").grid(row=1, column=0, padx=10, pady=10, sticky=E)
  85. dst_entry = Entry(window, width=50)
  86. dst_entry.grid(row=1, column=1, padx=10, pady=10)
  87. Button(window, text="...", command=select_dst, width=3).grid(row=1, column=2, padx=10, pady=10)  # Largura reduzida
  88.  
  89. bar = Progressbar(window, orient=HORIZONTAL, length=400, mode='determinate')
  90. bar.grid(row=2, column=0, columnspan=3, padx=10, pady=10)
  91.  
  92. percentLabel = Label(window, textvariable=percent).grid(row=3, column=0, columnspan=3)
  93. taskLabel = Label(window, textvariable=text).grid(row=4, column=0, columnspan=3)
  94.  
  95. button = Button(window, text="Copiar", command=start_thread).grid(row=5, column=2, pady=10)
  96.  
  97. window.mainloop()
  98.  
Tags: python
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement