Skip to content

Instantly share code, notes, and snippets.

@tpokorra
Last active March 25, 2021 21:35
Show Gist options
  • Save tpokorra/c5f340c03f9f3fc3250ac8a6b24794fa to your computer and use it in GitHub Desktop.
Save tpokorra/c5f340c03f9f3fc3250ac8a6b24794fa to your computer and use it in GitHub Desktop.
Arrange Photos into directories per day
#!/usr/bin/python3
import os
import pathlib
from datetime import datetime
for file in os.listdir("."):
if file.endswith(".jpg") or file.endswith(".3gp"):
if '_' in file:
date = datetime.strptime(file.split("_", 2)[1], '%Y%m%d')
datestr = date.strftime('%m/%d/%Y')
print(file + " " + datestr)
directory = os.path.join(os.getcwd(), date.strftime('%Y') + "-" + date.strftime('%m') + "-" + date.strftime('%d'))
if not os.path.exists(directory):
os.makedirs(directory)
os.rename (os.path.join(os.getcwd(), file), os.path.join(directory, file))
for file in os.listdir("."):
if file.endswith(".JPG") or file.endswith(".jpg") or file.endswith(".3gp"):
fname = pathlib.Path(file)
date = datetime.fromtimestamp(fname.stat().st_mtime)
datestr = date.strftime('%m/%d/%Y')
print(file + " " + datestr)
directory = os.path.join(os.getcwd(), date.strftime('%Y') + "-" + date.strftime('%m') + "-" + date.strftime('%d'))
if not os.path.exists(directory):
os.makedirs(directory)
os.rename (os.path.join(os.getcwd(), file), os.path.join(directory, file))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment