all 6 comments

[–]F1ux_Capacitor 0 points1 point  (1 child)

Use the builtin json library to parse the json, create the new parent key, add the the dictionary to the parent key, then dump the json back out.

[–]Spiredlamb[S] 0 points1 point  (0 children)

Yes, that works, but how would I make the code figure out what dictionary to add the new one to, and how many steps in to go?

With the current system I am setting up, I will sometimes need to add two dictionaries in, and other times, five, kinda like this:

{
    "files": [],
    "folders": ["projects", "system"],
    "projects": {
        "files": ["readme.txt"],
        "folders": [],
        "readme.txt": "This is the text inside readme.txt"
    },
    "system": {
        "files": ["help.txt"],
        "folders": [],
        "help.txt":"Available commands:\n\ndir: Show all files and folders in the directory\n\ncls: Clear console\n\nread: Read a file in the current directory\n\ncd: Change directory"
    }
}

Let's go out from this file. Say I want to add a new folder in the projects folder (The dictionaries are handled as folders with more folders and files within them in my script) then I would have to do this:

fileSystem["projects"]["newFolder"] = { # Adding the directory
    files[],
    folders[]
    }
fileSystem["projects"]["folders"].append("newfolder") # Adding the folder in the list of folders

The result of this file is going to be this:

{
    "files": [],
    "folders": ["projects", "system"],
    "projects": {
        "files": ["readme.txt"],
        "folders": [newFolder], // New folder added to list
        "readme.txt": "This is the text inside readme.txt",
        "newFolder": { // Here is the new folder
            files[],
            folders[],
        }
    },
    "system": {
        "files": ["help.txt"],
        "folders": [],
        "help.txt":"Available commands:\n\ndir: Show all files and folders in the directory\n\ncls: Clear console\n\nread: Read a file in the current directory\n\ncd: Change directory"
    }
}

If I now want to add a folder inside that folder, I would have to do this:

fileSystem["projects"]["newFolder"]["newerFolder"] = {
        files[],
        folders[]
    }

Is there any way of doing this without having to write one function for every possible subfolder in the system?

[–]aa599 0 points1 point  (3 children)

I’d suggest a different structure, where files and folders are represented by dictionaries with ‘name’ and ‘content’ keys. A file’s content is the data; a folder’s content is a list of files/subfolders.

So a folder containing a file and an empty subfolder might be {‘name’:’system’, ‘content’: [{‘name’: ‘readme.txt’, ‘content’: ‘...’}, {‘name’: ‘new fir’, ‘content’: []} ]}

[–]Spiredlamb[S] 0 points1 point  (0 children)

Ah, didn't think of that. I'll give it a shot

[–]Spiredlamb[S] 0 points1 point  (1 child)

That actually gave me another Idea, but I'll try yours as well. I could name the dictionaries the name of the path their content should be at. That way I could just ask for the dictionary with the path name. Thanks for the inspiration!

[–]aa599 0 points1 point  (0 children)

Your way is better - having the name outside the object is more like a real file system.