Mundo em Python

Organizar Pastas

import os
import shutil
from tkinter import * root = Tk()
root.geometry("550x400")
root.resizable(0, 0)
root.config(bg="#103030")
root.title("Organizar Pastas") titulo = Label(text="Organizar Pastas",
font=("Arial", "28", "bold"), bg="#103030", fg="#49e3e3")
titulo.place(relx=0.2, rely=0.05) texto_sub1 = Label(text="Caminho da pasta que deseja organizar",
font=("Arial", "18", "bold"), bg="#103030", fg="#49e3e3")
texto_sub1.place(relx=0.08, rely=0.25) pasta_deseja_organizar = StringVar()
pasta_deseja_organizar_entrada = Entry(textvariable=pasta_deseja_organizar,
font=("Arial", "12", "bold"),
bg="white", fg="blue", justify='center')
pasta_deseja_organizar_entrada.place(relx=0.05, rely=0.35, relwidth=0.9)
def limpar():
pasta_deseja_organizar_entrada.delete(0, END)
resultado_texto.config(text="")
def app():
pasta = pasta_deseja_organizar.get()
categorias = {
"Imagens": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"Documentos": [".pdf", ".docx", ".txt", ".xlsx", ".pptx"],
"Áudio": [".mp3", ".wav", ".aac"],
"Vídeos": [".mp4", ".mkv", ".mov", ".avi"],
"Comprimidos": [".zip", ".rar", ".7z", ".tar"],
"Outros": []
} # Verificar se a pasta existe
if not os.path.exists(pasta):
resultado_texto.config(text="A pasta não existe. Verifique o caminho.")
return

for categoria, extensoes in categorias.items():
caminho_categoria = os.path.join(pasta, categoria)
if not os.path.exists(caminho_categoria):
os.makedirs(caminho_categoria) for ficheiro in os.listdir(pasta):
caminho_ficheiro = os.path.join(pasta, ficheiro) if os.path.isdir(caminho_ficheiro):
continue

ficheiro_movido = False
for categoria, extensoes in categorias.items():
if any(ficheiro.lower().endswith(ext) for ext in extensoes):
shutil.move(caminho_ficheiro, os.path.join(pasta, categoria, ficheiro))
ficheiro_movido = True
break

if not ficheiro_movido:
shutil.move(caminho_ficheiro, os.path.join(pasta, "Outros", ficheiro)) resultado_texto.config(text="Organização concluída com sucesso!")
but1 = Button(text="Organizar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=app)
but1.place(relx=0.1, rely=0.65, relwidth=0.25, relheight=0.1) but_limpar = Button(text="Limpar", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=limpar)
but_limpar.place(relx=0.4, rely=0.65, relwidth=0.25, relheight=0.1) but_sair = Button(text="Sair", bd=2, bg='#107db2', fg='white',
font=('verdana', 12, 'bold'), command=root.destroy)
but_sair.place(relx=0.7, rely=0.65, relwidth=0.25, relheight=0.1)
resultado_texto = Label(text="",
font=("Arial", 12, "bold"), bg="#cfe2f3")
resultado_texto.place(relx=0.05, rely=0.8, relwidth=0.9, relheight=0.15) root.mainloop()
0