'create a root, parent, child, grandchild relationship in neo4j using python

Trying to create a root, parent, child and grandchild relationship - basically a recreation of a directory structure in windows.

The sub-folders depth is not set, so it can be as many as the user wants.

The root node location is passed in from the arguments, so that will be the starting point.

I am using this block of code to iterate through the directories and sub-directories

for subdir, dirs, files in os.walk(path):
    for file in files:
        print(os.path.join(subdir, file))
        filepath = subdir + os.sep + file
        sourcelocation = subdir + os.sep

the tree navigation appears to be working correctly, as I can print each directory

What the next step is where I am getting stuck.

I am able to parse the directory into the different items by using the split on os.sep

Question is - how do I establish a root -> parent -> child -> grandchild relationship?

I thought this would give it to me, but my thinking is incorrect

            db.run(f"CREATE (n:{x[0]} {{name: '{x[0]}'}})")
            db.run(f"CREATE (n:{x[4]} {{name: '{x[4]}'}})")
            db.run(f"CREATE (n:{x[5]} {{name: '{x[5]}'}})")
            db.run(f"CREATE (n:{x[6]} {{name: '{x[6]}'}})")

            db.run(f"MATCH (a:{x[0]}) CREATE (b {{name: '{outline}'}}) CREATE (a)-[:Child]->(b)")
            db.run(f"MATCH (c:{x[4]}) CREATE (d {{name: '{outline}'}}) CREATE (c)-[:Child]->(d)")
            db.run(f"MATCH (e:{x[5]}) CREATE (f {{name: '{outline}'}}) CREATE (d)-[:Child]->(f)")

basically, i want to recreate a directory structure from a starting directory and work the way down



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source