-
-
Save urjeetpatel/a4c1d564201a7041c8f789a7315b524b to your computer and use it in GitHub Desktop.
Python: move files to creation date named directories
This file contains hidden or 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
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changing to
os.path.join
andos.sep
for path operationsstr.format()
for string operations.