Member-only story

Create a Photo Organiser with Python by ChatGPT

GeoSense ✅
2 min readFeb 11, 2023

I like my photos organised into folders according to the month they were shot so that I can easily go back and view them or search for them. So I want this to be done automatically, with Python. Problem is defined! Let’s get started!

The full code is here:

import os
import shutil
import datetime

# path to the source directory
src_dir = 'photos'

# path to the destination directory for photos
photo_dest_dir = 'PT'

# path to the destination directory for videos
video_dest_dir = 'VD'

# list of file extensions for photos
photo_extensions = ['.jpg', '.jpeg', '.png', '.bmp','.heic']

# list of file extensions for videos
video_extensions = ['.mp4', '.avi', '.mkv', '.mov']

# iterate over all files in the source directory
for filename in os.listdir(src_dir):
# get the file creation time
file_ctime = os.path.getctime(os.path.join(src_dir, filename))
# convert the creation time to a datetime object
file_ctime = datetime.datetime.fromtimestamp(file_ctime)
# get the file extension
file_extension = os.path.splitext(filename)[1].lower()

# check if the file is a photo
if file_extension in photo_extensions:
# create the destination directory for the photo based on its creation date
dest_dir = os.path.join(photo_dest_dir, str(file_ctime.year), str(file_ctime.month))
# create the destination directory if it doesn't exist
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# move the photo to the destination directory
shutil.move(os.path.join(src_dir, filename), os.path.join(dest_dir, filename))

# check if the file is a video
if file_extension in video_extensions:
# create the destination directory for the video based on its creation date
dest_dir = os.path.join(video_dest_dir, str(file_ctime.year), str(file_ctime.month))
# create the destination directory if it doesn't exist
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
# move the video to the destination directory
shutil.move(os.path.join(src_dir, filename), os.path.join(dest_dir, filename))

Link GitHub for this chapter here.

Hope this is helpful!

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

GeoSense ✅
GeoSense ✅

Written by GeoSense ✅

🌏 Remote sensing | 🛰️ Geographic Information Systems (GIS) | ℹ️ https://www.tnmthai.com/medium

No responses yet

Write a response