4

I would like to store a JSON structure which holds projects and files I am currently working on. File structure looks like this:

|-project1
|--sequence1
|----file1.ext
|----file2.ext
|-project2
|--sequence1
|----file1.ext
|----file2.ext
|-project3
|--sequence3
|----file1.ext
|----file2.ext

JSON version would look like this:

data = [
  {
    type: "folder",
    name: "project1",
    path: "/project1",
    children: [
      {
        type: "folder",
        name: "sequence1",
        path: "/project1/sequence1",
        children: [
          {
            type: "file",
            name: "file1.ext",
            path: "/project1/sequence1/file1.ext"
          } , {
            type: "file",
            name: "file2.ext",
            path: "/project1/sequence1/file2.ext"

            ...etc
          }
        ]
      }
    ]
  }
]

Is it possible to render this structure to actual directories and files using Python? (it can be empty files). On the other hand, I would like to build a function traversing existing directories, returning similar JSON code.

1
  • you can use the 'json' module (part of the standard library) and the functions 'load' or 'loads' to load the data from a file or a string as a python dictionary. Commented Oct 9, 2014 at 21:59

2 Answers 2

3

This should be easy enough to create the dictionary using a recursive function and some of the goodies in the os module...:

import os

def dir_to_list(dirname, path=os.path.pathsep):
    data = []
    for name in os.listdir(dirname):
        dct = {}
        dct['name'] = name
        dct['path'] = path + name

        full_path = os.path.join(dirname, name)
        if os.path.isfile(full_path):
            dct['type'] = 'file'
        elif os.path.isdir(full_path):
            dct['type'] = 'folder'
            dct['children'] = dir_to_list(full_path, path=path + name + os.path.pathsep)
        data.append(dct)
    return data

untested

Then you can just json.dump it to a file or json.dumps it to a string. . .

Sign up to request clarification or add additional context in comments.

3 Comments

Am I missing something or will data here always be empty as it is never filled? There are also some variable inconsistencies with full_path and fullpath.
I did say it was untested :)
Ah, I didn't see that. The lettering was too small :p Gave it a short test, from a first glance the output looks quite good :)
0

If you happen to use Unix based system, you could use "tree" command

$ tree -Jd . > folderTemplate.json

or from Python:

import subprocess
data = subprocess.run(["tree", "-Jd", "/path/to/dir"], stdout=subprocess.PIPE)
print(data.stdout)

Then it would be simple to convert it back to directory structure with Python. Maybe this might be helpful: https://github.com/tmdag/makedirtree

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.