mayankjoin3

organize image python code img_month_wise-sort_photos

Jun 22nd, 2026
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.93 KB | None | 0 0
  1. import os
  2. import re
  3. import shutil
  4. import calendar
  5. from datetime import datetime
  6.  
  7. # Base path
  8. base_path = r"D:\OneDrive\Photos Collection Since 2007\2017\Honor Israel 6x 2017"
  9. timestamp = datetime.now().strftime("%Y_%m_%d")
  10. log_file = os.path.join(base_path, f"organizer_log_{timestamp}.txt")
  11.  
  12. # List of regex patterns for jpg filenames
  13. patterns = [
  14.     re.compile(r"^IMG_(\d{8})_\d{6}\.jpg$", re.IGNORECASE),
  15.     re.compile(r"^IMG_(\d{8})_\d{9}\.jpg$", re.IGNORECASE),
  16.     re.compile(r"^IMG_(\d{8})_\d{9}\_HDR.jpg$", re.IGNORECASE),
  17.     re.compile(r"^IMG_(\d{8})_\d{6}_\d+\.jpg$", re.IGNORECASE),
  18.    
  19.     # NEW PATTERNS ADDED HERE FOR AGC FILES
  20.     # Matches AGC_YYYYMMDD_SEQUENCE.jpg (e.g., AGC_20240422_185307583.jpg)
  21.     re.compile(r"^AGC_(\d{8})_\d{9}\.jpg$", re.IGNORECASE),
  22.     # Matches AGC_YYYYMMDD_SEQUENCE.NIGHT.jpg (e.g., AGC_20241017_184801783.NIGHT.jpg)
  23.     re.compile(r"^AGC_(\d{8})_\d{9}\.NIGHT\.jpg$", re.IGNORECASE),
  24.  
  25.     re.compile(r"^PXL_(\d{8})_.*\.jpg$", re.IGNORECASE),
  26.     re.compile(r"^PXL_(\d{8})_\d{9}_\d+\.jpg$", re.IGNORECASE),
  27.     re.compile(r"^PHOTO_(\d{8})_\d{6}\.jpg$", re.IGNORECASE),
  28.     re.compile(r"^PIC(\d{8})_\d{6}\.jpg$", re.IGNORECASE)
  29. ]
  30. # Function to check against multiple patterns
  31. def match_patterns(filename):
  32.     for p in patterns:
  33.         m = p.match(filename)
  34.         if m:
  35.             return m
  36.     return None
  37.  
  38. # Function to write logs
  39. def write_log(message):
  40.     timestamp = datetime.now().strftime("[%Y-%m-%d %H:%M:%S]")
  41.     with open(log_file, "a", encoding="utf-8") as f:
  42.         f.write(f"{timestamp} {message}\n")
  43.  
  44. try:
  45.     # Walk recursively through the directory
  46.     for root, dirs, files in os.walk(base_path):
  47.         for file in files:
  48.             try:
  49.                 file_path = os.path.join(root, file)
  50.  
  51.                 # Skip if file is already in the target folders (avoid infinite loops)
  52.                 if any(folder in file_path.lower() for folder in ["\\others\\", "\\temp\\"]):
  53.                     continue
  54.  
  55.                 # Check if file matches any of the jpg patterns
  56.                 match = match_patterns(file)
  57.                 if match:
  58.                     date_str = match.group(1)  # YYYYMMDD
  59.                     year = date_str[:4]
  60.                     month_num = date_str[4:6]
  61.                     month_name = calendar.month_name[int(month_num)]
  62.                     month_folder = f"{month_num} - {month_name}"
  63.  
  64.                     # Destination: d:\photos\year\MM - Month
  65.                     dest_dir = os.path.join(base_path, year, month_folder)
  66.                     os.makedirs(dest_dir, exist_ok=True)
  67.                     dest_path = os.path.join(dest_dir, file)
  68.  
  69.                     shutil.move(file_path, dest_path)
  70.                     write_log(f"MOVED JPG (pattern) → {dest_path}")
  71.  
  72.                 elif file.lower().endswith(".jpg"):
  73.                     # If JPG but not matching pattern → move to temp
  74.                     temp_dir = os.path.join(base_path, "temp")
  75.                     os.makedirs(temp_dir, exist_ok=True)
  76.                     dest_path = os.path.join(temp_dir, file)
  77.  
  78.                     shutil.move(file_path, dest_path)
  79.                     write_log(f"MOVED JPG (no pattern) → {dest_path}")
  80.  
  81.                 else:
  82.                     # Other file types → move to d:\photos\others\ext
  83.                     ext = os.path.splitext(file)[1].lower().lstrip(".")
  84.                     if not ext:  # No extension
  85.                         ext = "noext"
  86.  
  87.                     dest_dir = os.path.join(base_path, "others", ext)
  88.                     os.makedirs(dest_dir, exist_ok=True)
  89.                     dest_path = os.path.join(dest_dir, file)
  90.  
  91.                     shutil.move(file_path, dest_path)
  92.                     write_log(f"MOVED OTHER ({ext}) → {dest_path}")
  93.  
  94.             except Exception as e:
  95.                 write_log(f"ERROR processing file {file}: {e}")
  96.  
  97. except Exception as e:
  98.     write_log(f"FATAL ERROR: {e}")
  99.  
Advertisement
Add Comment
Please, Sign In to add comment