Skip to content

Instantly share code, notes, and snippets.

@hybridjosto
Created April 20, 2014 09:45
Show Gist options
  • Save hybridjosto/11109927 to your computer and use it in GitHub Desktop.
Save hybridjosto/11109927 to your computer and use it in GitHub Desktop.
saves folder structure to json file allowing it to be recovered
#! /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(local=False):
with open('folders.json', 'rb') as f:
jsonTree = json.load(f)
for paths in jsonTree['folders']:
if not local:
if not os.path.exists(paths):
os.makedirs(paths)
else:
print paths
else:
os.makedirs(paths.replace('T:','C:'))
if __name__ == '__main__':
inp_folder = 'C:\\'
saveTree(inp_folder)
# replaceFolders(local=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment