Skip to content

Instantly share code, notes, and snippets.

@shawnbutts
Created April 30, 2016 20:19
Show Gist options
  • Save shawnbutts/c51275e7c2fcf153e36dacfc16ab0407 to your computer and use it in GitHub Desktop.
Save shawnbutts/c51275e7c2fcf153e36dacfc16ab0407 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import time
import datetime
def get_directory_structure(rootdir, strip=False):
"""
Creates a nested dictionary that represents the folder & file
structure of rootdir
"""
ret = []
for path, dirs, files in os.walk(rootdir):
a = {}
if strip:
p = path.strip(rootdir + os.sep)
else:
p = path
if len(p.split(os.sep)) == 1:
parent = ''
if len(p.split(os.sep)) > 1:
parent = os.sep.join(p.split(os.sep)[:-1])
if path == rootdir:
parent = 'root'
a['path'] = p
a['fullpath'] = path
a['parent'] = parent
a['dirs'] = dirs
a['files'] = []
for file in files:
f = {}
ff = path + os.sep + file
(mode, ino, dev, nlink, uid, gid, size,
atime, mtime, ctime) = os.stat(ff)
f['name'] = file
f['mode'] = mode
f['ino'] = ino
f['dev'] = dev
f['nlink'] = nlink
f['uid'] = uid
f['gid'] = gid
f['size'] = size
f['atime'] = atime
f['mtime'] = mtime
f['ctime'] = ctime
a['files'].append(f)
ret.append(a)
return(ret)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment