import os
import arrow
import json
import itertools

print(os.getcwd())
os.chdir('./os_exercise')
name = os.listdir()
title = ['csv', 'json', 'log', 'txt']
new = []

l = []
for line in os.listdir():
    sta = os.stat(line)
    date = arrow.get(sta.st_mtime).format('MMMM DD, YYYY')
    size = sta.st_size
    gid = sta.st_gid
    uid = sta.st_uid

    data = {
        'name': line,
        'last_modified': date,
        'size': size,
        'group_id': gid,
        'user_id': uid
    }
    new.append(data)
new.sort(key=lambda new:new['name'])

with open('new_data.json', 'w') as json_file:
    dic = {}    
    for key, group in itertools.groupby(new, key=lambda each: each['name'].split('.')[1]):
        
        #print(res)
        dic[key] = list(group)
    json.dump(dic, json_file, indent=2)