Skip to content

Instantly share code, notes, and snippets.

@urjeetpatel
Forked from vadviktor/move.py
Last active February 4, 2017 07:34
Show Gist options
  • Save urjeetpatel/a4c1d564201a7041c8f789a7315b524b to your computer and use it in GitHub Desktop.
Save urjeetpatel/a4c1d564201a7041c8f789a7315b524b to your computer and use it in GitHub Desktop.
Python: move files to creation date named directories
#!/usr/bin/python3
import os, time, shutil, sys, calendar
dir = sys.argv[1]
os.chdir(dir)
for f in filter(os.path.isfile, os.listdir(os.curdir):
ftime = time.gmtime(os.path.getmtime(f))
ctime_dir = "{1}{0}{2}".format(os.sep,
ftime.tm_year,
calendar.month_name[ftime.tm_mon])
os.makedirs(ctime_dir,exist_ok=True)
dst = os.path.join(ctime_dir, f)
shutil.move(f, dst);
print('File {0} has been moved to {1}'.format(f,dst))
@urjeetpatel
Copy link
Author

urjeetpatel commented Feb 4, 2017

Changing to

  1. Ignore Directories in the current path
  2. Use platform agnostic os.path.join and os.sep for path operations
  3. Use str.format() for string operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment