Created
February 17, 2019 16:08
-
-
Save wwwins/ef4b7fab44ff4fd7e5dda6dba8f2d0bb to your computer and use it in GitHub Desktop.
Move files to folder by date
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/bin/python | |
# | |
# Copyright 2019 isobar. All Rights Reserved. | |
# | |
# Move files to folder by date | |
# | |
# Usage: | |
# mv2folder.py . | |
# | |
import os | |
import sys | |
import time | |
import glob | |
import shutil | |
def mv2folder(folder): | |
for f in os.listdir(folder): | |
if os.path.isdir(f): | |
mv2folder(os.path.join('.',f)) | |
else: | |
ftime = time.gmtime(os.path.getmtime(os.path.join(folder,f))) | |
mon = '0'+str(ftime.tm_mon) | |
dir = str(ftime.tm_year)+'_'+mon[-2:] | |
if not os.path.isdir(dir): | |
os.mkdir(dir) | |
dst = os.path.join(dir,f) | |
src = os.path.join(folder,f) | |
shutil.move(src, dst) | |
print(src+' move to '+dst) | |
folder = sys.argv[1] | |
os.chdir(folder) | |
mv2folder(folder) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment