Created
May 7, 2018 22:57
-
-
Save alanwill/5b9cedca8875a66c4f5455a6477d24c2 to your computer and use it in GitHub Desktop.
Copies files to a date centric directory structure based on value of a json field within the file
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 os, json, shutil | |
from datetime import datetime | |
rootdir ='./input' | |
for subdir, dirs, files in os.walk(rootdir): | |
#print(subdir, dirs, files) | |
for file in files: | |
print(file) | |
f=open(subdir + '/'+ file,'r') | |
lines=f.readlines() | |
json_data = json.loads(lines[0]) | |
#print(json_data['dateContactWarehoused']) | |
date = datetime.strptime(json_data['dateContactWarehoused'], "%Y-%m-%dT%H:%M:%S.%fZ") | |
year = str(date.year) | |
month = str(date.month) | |
day = str(date.day) | |
#print(os.getcwd()) | |
dest_path = os.getcwd() + '/output/' + year + '/' + month + '/' + day + '/' | |
os.makedirs(os.path.dirname(dest_path), exist_ok=True) | |
shutil.copy2(subdir + '/'+ file, dest_path) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment