Guest User

Python Sort

a guest
Jan 11th, 2025
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.86 KB | None | 0 0
  1. import os
  2. import shutil
  3.  
  4. # Define the target folders for sorting
  5. VIDEO_EXTENSIONS = {'.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.mpeg', '.mpg'}
  6. RAW_PHOTO_EXTENSIONS = {'.cr2', '.nef', '.arw', '.dng', '.raf', '.orf', '.sr2'}
  7. JPG_EXTENSIONS = {'.jpg', '.jpeg'}
  8.  
  9. def sort_files(drive_path):
  10.     # Ensure the destination folders exist
  11.     video_folder = os.path.join(drive_path, "Video files")
  12.     raw_photos_folder = os.path.join(drive_path, "RAW Photos files")
  13.     jpg_folder = os.path.join(drive_path, "JPG files")
  14.    
  15.     for folder in [video_folder, raw_photos_folder, jpg_folder]:
  16.         if not os.path.exists(folder):
  17.             os.makedirs(folder)
  18.  
  19.     # Walk through the directory
  20.     for root, _, files in os.walk(drive_path):
  21.         for file in files:
  22.             file_path = os.path.join(root, file)
  23.             file_extension = os.path.splitext(file)[1].lower()
  24.  
  25.             # Move files into the appropriate folder based on extension
  26.             if file_extension in VIDEO_EXTENSIONS:
  27.                 target_folder = video_folder
  28.             elif file_extension in RAW_PHOTO_EXTENSIONS:
  29.                 target_folder = raw_photos_folder
  30.             elif file_extension in JPG_EXTENSIONS:
  31.                 target_folder = jpg_folder
  32.             else:
  33.                 continue  # Skip files that don't match the extensions
  34.  
  35.             # Move the file to the appropriate folder
  36.             target_file_path = os.path.join(target_folder, file)
  37.             try:
  38.                 shutil.move(file_path, target_file_path)
  39.                 print(f"Moved {file} to {target_folder}")
  40.             except Exception as e:
  41.                 print(f"Error moving file {file}: {e}")
  42.  
  43. if __name__ == "__main__":
  44.     # Replace with the path to your external hard drive
  45.     drive_path = r"F:\YourDrive"  # Adjust this path accordingly
  46.     sort_files(drive_path)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment