Created
December 30, 2019 03:33
-
-
Save Roxxers/c9e15b99a24b0785fd41a05148d73ab4 to your computer and use it in GitHub Desktop.
Concatanates google photo export folders from daily to monthly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import datetime | |
# For this to work as accurately as possible, you need to remove any folders that might span between the | |
# end of one month and the beginning of the next | |
def googledate_to_datetime(time: str) -> datetime.datetime: | |
# Get rid of any patterns like 2019-06-19 #2 | |
time = time.split(" ")[0] | |
# Get rid of any patterns like 2019-06-19-20 | |
time = "-".join(time.split("-")[0:3]) | |
return datetime.datetime.strptime(time, "%Y-%m-%d") | |
BASE_DIR = os.getcwd() | |
directories = glob.glob(BASE_DIR + "/*") | |
for directory in directories: | |
filename = directory.split("/")[-1] | |
try: | |
date = googledate_to_datetime(filename) | |
folder = date.strftime("%Y-%m") | |
try: | |
os.mkdir(folder) | |
except FileExistsError: | |
pass | |
for subfile in os.listdir(directory): | |
os.rename(f"{directory}/{subfile}", | |
f"{BASE_DIR}/{folder}/{subfile}") | |
print(f"Moved {subfile} to {folder}") | |
except ValueError: | |
# Skipping folders that cannot be converted to dates | |
pass | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment