I have a huge Google Photos directory that is difficult to navigate - there are just too many files so I thougt I need to sort them somehow. I decided to make it simple and group them by date-taken. As I always wanted to learn python I thought I'll give it a try so this is my first take on it.
There is no rocket-science here (yet). It looks for files in the pictures_path
, groups them by their modified date, enumerates this iterator, creates directories and moves or copies files according to the whatif
option. This is measured by the perf_counter()
.
I'd be great if you could take a look at it and tell me whether I did something terribly wrong as a beginner before I let it work with my actual collection.
import os
import time
import itertools
import shutil
from pprint import pprint
def format_filemtime(path):
filemtime = os.path.getmtime(path)
return time.strftime('%Y-%m-%d', time.gmtime(filemtime))
def group_pictures(whatif=False):
pictures_path = "C:\\temp\\picturepy\\pictures\\"
galleries_path = "C:\\temp\\picturepy\\galleries\\"
start = time.perf_counter()
picture_names = os.listdir(pictures_path)
print(f"picture count: {len(picture_names)}")
pictures_by_mtime = itertools.groupby(picture_names, lambda name: format_filemtime(os.path.join(pictures_path,name)))
for (dir, picture_names) in pictures_by_mtime:
path_dir = os.path.join(galleries_path, dir)
if not os.path.exists(path_dir):
os.makedirs(path_dir)
print(f"'{dir}' created.")
else:
print(f"'{dir}' already exists.")
do = shutil.copyfile if whatif else shutil.move
verb = "copied" if whatif else "moved"
for file in picture_names:
do(
src=os.path.join(pictures_path, file),
dst=os.path.join(path_dir, file))
print(f"\t'{file}' {verb}.")
end = time.perf_counter()
elapsed = round(end - start,2)
print(f"elapsed: {elapsed} sec")
# --- --- ---
def main():
group_pictures(whatif=True)
#group_pictures()
if __name__ == '__main__':
main()
I tested it with VSCode
and it's doing its job pretty well.