Skip to content

Instantly share code, notes, and snippets.

@seanh
Created Jan 25, 2020
Embed
What would you like to do?
Move the contents of ~/Desktop into today's day folder
#!/usr/bin/env python3
"""Move the contents of ~/Desktop into ~/Notes/DAY_FOLDERS/YYYY/MM/DD/.
Create today's day folder (~/Notes/DAY_FOLDERS/YYYY/MM/DD/) and its parent
folders if they don't exist already, and then move everything from ~/Desktop
into today's day folder.
If ~/Desktop is already empty then don't do anything (don't create an empty day
folder).
May overwrite pre-existing files in the day folder with files from ~/Desktop.
Will not merge pre-existing subdirectories. If there's a ~/Desktop/FOO/
directory and there's a pre-existing <TODAYS_DAY_FOLDER>/FOO/ directory then
this script will crash.
"""
import datetime
import os
import shutil
import sys
today = datetime.date.today()
home_folder = os.path.expanduser("~")
desktop_folder = os.path.join(home_folder, "Desktop")
day_folder = os.path.join(
home_folder,
"Notes",
"DAY_FOLDERS",
today.strftime("%Y"),
today.strftime("%m"),
today.strftime("%d"),
)
names = os.listdir(desktop_folder)
if not names:
# If there are no files on the desktop then exit now so we don't create an empty day folder.
sys.exit()
os.makedirs(day_folder, exist_ok=True)
for name in names:
src = os.path.join(desktop_folder, name)
if os.path.isfile(src):
dst = os.path.join(day_folder, name)
else:
dst = day_folder
shutil.move(src, dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment