-
-
Save hendrikleper/00b71b65edc8e63232d46525cce7a610 to your computer and use it in GitHub Desktop.
Python: move files to creation date named directories
This file contains 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/local/bin/python3 | |
#use with 'python move.py /myDirectory/' | |
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