Skip to content

Instantly share code, notes, and snippets.

@hybridjosto
Created April 2, 2014 06:19
Show Gist options
  • Save hybridjosto/9928836 to your computer and use it in GitHub Desktop.
Save hybridjosto/9928836 to your computer and use it in GitHub Desktop.
#! /usr/bin/env python
import os
import json
import time
def listFolders(folderpath):
"""
Iterates through the child folders of folderpath
and adds items to a list object.
"""
folders = []
for path, dirs, files in os.walk(folderpath):
folders.append(path)
return folders
def saveTree(basefolder):
"""
creates a file called folders.json with a dictionary of the
folders created in the listFolders function
"""
tree = listFolders(basefolder)
today = time.strftime("%Y-%m-%d %H:%M:%S")
treeDict = {'date': today, 'folders': tree}
jsonTree = json.dumps(treeDict, indent=4)
with open('folders.json', 'wb') as f:
f.write(jsonTree)
def replaceFolders():
with open('folders.json', 'rb') as f:
jsonTree = json.load(f)
for paths in jsonTree['folders']:
if not os.path.exists(paths):
os.makedirs(paths)
else:
print paths
if __name__ == '__main__':
# inp_folder = '/Users/Josh/Desktop/'
# saveTree(inp_folder)
# replaceFolders()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment