Table of Contents

<< Back

Background

The History of Programming - [DevCon 2016]
John Backus

C++

Learn CCP Mini Project: How to program a GPU? | CUDA C/C++

Lua

lua.org

Pyhon

Usecases

Copy all images from folder and child folders

  1. import os
  2. import shutil
  3. def copy_images_and_parent_folder(source_folder, destination_folder):
  4. # Create the destination folder if it doesn't exist
  5. if not os.path.exists(destination_folder):
  6. os.makedirs(destination_folder)
  7. # Walk through the source folder and its subfolders
  8. for root, dirs, files in os.walk(source_folder):
  9. for file in files:
  10. # Check if the file is an image (based on file extension)
  11. if file.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
  12. source_path = os.path.join(root, file)
  13. destination_path = os.path.join(destination_folder, file)
  14. shutil.copy2(source_path, destination_path)
  15. print(f"Copied {file} to {destination_folder}")
  16. # Copy the parent folder from the source to the destination
  17. parent_folder = os.path.dirname(source_folder)
  18. parent_folder_name = os.path.basename(parent_folder)
  19. destination_parent_folder = os.path.join(destination_folder, parent_folder_name)
  20. if not os.path.exists(destination_parent_folder):
  21. shutil.copytree(parent_folder, destination_parent_folder)
  22. print(f"Copied parent folder: {parent_folder_name}")
  23. # Set the source folder path (change to the desired folder)
  24. source_folder_path = "/path/to/your/source/folder"
  25. # Set the destination folder path (create a new folder or use an existing one)
  26. destination_folder_path = "/path/to/your/destination/folder"
  27. # Call the function to copy images and the parent folder
  28. copy_images_and_parent_folder(source_folder_path, destination_folder_path)
  29. }();

Replace “/path/to/your/source/folder” with the actual path to the folder containing your images, and “/path/to/your/destination/folder” with the desired destination folder. This script will copy image files source to the destination.

Delete all '.DS_Store' or/and 'thumbs.db' files

  1. import os
  2. import tkinter as tk
  3. from tkinter import filedialog, messagebox, ttk
  4.  
  5. def remove_files(folder_path, file_types):
  6. removed_count = {file_type: 0 for file_type in file_types}
  7. for root, dirs, files in os.walk(folder_path):
  8. for file in files:
  9. if file.lower() in file_types:
  10. file_path = os.path.join(root, file)
  11. try:
  12. os.remove(file_path)
  13. removed_count[file.lower()] += 1
  14. except Exception as e:
  15. print(f"Error removing {file_path}: {e}")
  16. return removed_count
  17.  
  18. def select_folder():
  19. folder_path = filedialog.askdirectory()
  20. if folder_path:
  21. folder_var.set(folder_path)
  22.  
  23. def clean_folder():
  24. folder_path = folder_var.get()
  25. if not folder_path:
  26. messagebox.showerror("Error", "Please select a folder first.")
  27. return
  28.  
  29. file_types = []
  30. if ds_store_var.get():
  31. file_types.append(".ds_store")
  32. if thumbs_db_var.get():
  33. file_types.append("thumbs.db")
  34.  
  35. if not file_types:
  36. messagebox.showerror("Error", "Please select at least one file type to clean.")
  37. return
  38.  
  39. removed_count = remove_files(folder_path, file_types)
  40.  
  41. message = "Cleanup Complete:\n"
  42. for file_type, count in removed_count.items():
  43. message += f"Removed {count} {file_type} files\n"
  44.  
  45. messagebox.showinfo("Cleanup Result", message)
  46.  
  47. # Create the main window
  48. root = tk.Tk()
  49. root.title("Folder Cleaner")
  50. root.geometry("400x220")
  51.  
  52. # Create and set up widgets
  53. folder_var = tk.StringVar()
  54. ds_store_var = tk.BooleanVar(value=True)
  55. thumbs_db_var = tk.BooleanVar(value=True)
  56.  
  57. frame = ttk.Frame(root, padding="10")
  58. frame.grid(row=0, column=0, sticky=(tk.W, tk.E, tk.N, tk.S))
  59. root.columnconfigure(0, weight=1)
  60. root.rowconfigure(0, weight=1)
  61.  
  62. ttk.Label(frame, text="Select a folder to clean:").grid(column=0, row=0, sticky=tk.W, pady=5)
  63. ttk.Entry(frame, textvariable=folder_var, width=50).grid(column=0, row=1, sticky=(tk.W, tk.E), pady=5)
  64. ttk.Button(frame, text="📁 Browse", command=select_folder).grid(column=1, row=1, sticky=tk.W, padx=5, pady=5)
  65.  
  66. ttk.Label(frame, text="Select file types to clean:").grid(column=0, row=2, sticky=tk.W, pady=5)
  67. ttk.Checkbutton(frame, text=".DS_Store files", variable=ds_store_var).grid(column=0, row=3, sticky=tk.W)
  68. ttk.Checkbutton(frame, text="Thumbs.db files", variable=thumbs_db_var).grid(column=0, row=4, sticky=tk.W)
  69.  
  70. ttk.Button(frame, text="🧹 Clean Folder", command=clean_folder).grid(column=0, row=5, sticky=tk.W, pady=10)
  71.  
  72. # Start the GUI event loop
  73. root.mainloop()