Created
April 3, 2017 23:31
-
-
Save oreh/1e3329117e24eefb6b1d4baa24c17473 to your computer and use it in GitHub Desktop.
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
import sys, os, shutil | |
import datetime | |
dir_register = set() | |
def run(in_dir, out_dir): | |
for root, dirs, files in os.walk(in_dir): | |
for f in files: | |
_path = os.path.join(root, f) | |
_ctime = os.path.getmtime(_path) | |
dt = datetime.datetime.fromtimestamp(_ctime) | |
year = dt.year | |
month = dt.strftime('%b') | |
day = dt.day | |
base_name = os.path.basename(f) | |
new_dir = os.path.join(out_dir, str(year), month, str(day)) | |
if new_dir not in dir_register: | |
if not os.path.exists(new_dir): | |
os.makedirs(new_dir) | |
dir_register.add(new_dir) | |
new_path = os.path.join(new_dir, base_name) | |
shutil.move(_path, new_path) | |
if __name__ == "__main__": | |
in_dir = 'tmp' | |
out_dir = 'sorted' | |
run(in_dir, out_dir) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment