Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import shutil
- # Define the target folders for sorting
- VIDEO_EXTENSIONS = {'.mp4', '.mkv', '.avi', '.mov', '.wmv', '.flv', '.mpeg', '.mpg'}
- RAW_PHOTO_EXTENSIONS = {'.cr2', '.nef', '.arw', '.dng', '.raf', '.orf', '.sr2'}
- JPG_EXTENSIONS = {'.jpg', '.jpeg'}
- def sort_files(drive_path):
- # Ensure the destination folders exist
- video_folder = os.path.join(drive_path, "Video files")
- raw_photos_folder = os.path.join(drive_path, "RAW Photos files")
- jpg_folder = os.path.join(drive_path, "JPG files")
- for folder in [video_folder, raw_photos_folder, jpg_folder]:
- if not os.path.exists(folder):
- os.makedirs(folder)
- # Walk through the directory
- for root, _, files in os.walk(drive_path):
- for file in files:
- file_path = os.path.join(root, file)
- file_extension = os.path.splitext(file)[1].lower()
- # Move files into the appropriate folder based on extension
- if file_extension in VIDEO_EXTENSIONS:
- target_folder = video_folder
- elif file_extension in RAW_PHOTO_EXTENSIONS:
- target_folder = raw_photos_folder
- elif file_extension in JPG_EXTENSIONS:
- target_folder = jpg_folder
- else:
- continue # Skip files that don't match the extensions
- # Move the file to the appropriate folder
- target_file_path = os.path.join(target_folder, file)
- try:
- shutil.move(file_path, target_file_path)
- print(f"Moved {file} to {target_folder}")
- except Exception as e:
- print(f"Error moving file {file}: {e}")
- if __name__ == "__main__":
- # Replace with the path to your external hard drive
- drive_path = r"F:\YourDrive" # Adjust this path accordingly
- sort_files(drive_path)
Advertisement
Add Comment
Please, Sign In to add comment