''' Created on Oct 22, 2015 @author: Brett Paufler Copyright Brett Paufler #SO, still working on this, never got very far graph_from_file_path_list returns a networkX graph, given list_of file_paths This part works from there, want to get a tree like graph Long Term, this should be of value Intent it to take a directory listing, web site listing, or any hierarchical structure and produce Tree Structure (mostly done, I think) Tree Diagram ''' import networkx as nx import matplotlib.pyplot as plt #Test Data a = r''' C:\xampp\tomcat\webapps\examples\jsp\jsptoserv\jts.html C:\xampp\tomcat\webapps\examples\jsp\jsptoserv\ServletToJsp.java.html C:\xampp\tomcat\webapps\examples\jsp\num\numguess.html C:\xampp\tomcat\webapps\examples\jsp\num\numguess.jsp C:\xampp\tomcat\webapps\examples\jsp\num\numguess.jsp.html C:\xampp\tomcat\webapps\examples\jsp\plugin\plugin.html C:\xampp\tomcat\webapps\examples\jsp\plugin\plugin.jsp C:\xampp\tomcat\webapps\examples\jsp\plugin\plugin.jsp.html C:\xampp\tomcat\webapps\examples\jsp\plugin\applet\Clock2.class C:\xampp\tomcat\webapps\examples\jsp\plugin\applet\Clock2.java C:\xampp\tomcat\webapps\examples\jsp\security\protected\error.jsp ''' def nodes_from_path(file_path): '''Returns a list of nodes as derived from passed file_path. a/b/c.d in returns [a/, a/b/, a/b/c.d].''' nodes = [file_path] while file_path.rfind('\\') > 0: file_path = file_path[:file_path.rfind('\\')+1] nodes.append(file_path) file_path = file_path[:-1] #for n in nodes: # print n return nodes def edges_from_nodes(nodes): '''Returns a list of node pairs, given a list of connected nodes. [a, b, c] returns [(a,b), (b,c)].''' edges = [] for i in range(len(nodes) - 1): edges.append((nodes[i], nodes[i+1])) #for e in edges: # print e return edges def graph_from_file_path_list(list_of_file_paths): '''Returns a networkX graph, given a list_of_file_paths.''' #So, with a little nudging, should work for a directory walk, #but not tested at this time G = nx.Graph() for file_path in list_of_file_paths: print 'Adding to Graph: ', file_path path_nodes = nodes_from_path(file_path) path_edges = edges_from_nodes(path_nodes) G.add_nodes_from(path_nodes) G.add_edges_from(path_edges) return G if __name__ == '__main__': list_of_file_paths = a.split() #nodes = nodes_from_path(path) #edges = edges_from_nodes(nodes) G = graph_from_file_path_list(list_of_file_paths) nx.draw(G, with_labels=True) plt.show() #graph_tree(nodes, edges) print 'end'