Skip to content

Instantly share code, notes, and snippets.

@ikluhsman
Forked from vadviktor/move.py
Last active July 8, 2019 13:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ikluhsman/986a136c8004908dfec54d30fb1c491b to your computer and use it in GitHub Desktop.
Save ikluhsman/986a136c8004908dfec54d30fb1c491b to your computer and use it in GitHub Desktop.
Python: move files to creation date named directories
#!/usr/local/bin/python3
import os, time, shutil, sys
dir = sys.argv[1]
os.chdir(dir)
for f in os.listdir('.'):
ftime = time.gmtime(os.path.getmtime(f))
year = str(ftime.tm_year)
mon = str(ftime.tm_mon)
day = str(ftime.tm_mday)
if len(mon) == 1: mon = "0" + mon
if len(day) == 1: day = "0" + day
ctime_dir = year + mon + day
if not os.path.isdir(ctime_dir):
os.mkdir(ctime_dir)
dst = ctime_dir + "/" + f
shutil.move(f, dst);
print("File " + f + "has been moved to " + dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment