title: Filesytem tree to json
body: <p>I'm trying to convert a filesystem tree to json using python.</p>
  
  <p>Imagine that i have the following tree :</p>
  
  <pre><code>plans/
  |-- p1/
  |   |-- p1_1.pdf
  |   |-- p1_2.pdf
  |   `-- test/
  |       `-- test.jpg
  |-- p2/
  |   |-- p2_1.pdf
  |   |-- p2_2.pdf
  |   `-- test2/
  |
  `-- resume.pdf
  </code></pre>
  
  <p>I would like to have a json output like that :</p>
  
  <pre><code>[
  {
      "name": "p1",
      "type": "folder",
      "path": "/plans/p1",
      "tag": "org",
      "children": [
          {
              "name": "p1_1.pdf",
              "type": "file",
              "path": "/plans/p1/p1_1.pdf",
              "tag": "org"
          },
          {
              "name": "p1_2.pdf",
              "type": "file",
              "path": "/plans/p1/p1_2.pdf",
              "tag": "org"
          },
          {
              "name": "test",
              "type": "folder",
              "path": "/plans/p1/test",
              "tag": "org",
              "children": [
                  {
                      "name": "test.jpg",
                      "type": "file",
                      "path": "/plans/p1/test/test.jpg",
                      "tag": "org"
                  }
              ]
          }
      ]
  },
  {
      "name": "p2",
      "type": "folder",
      "path": "/plans/p2",
      "tag": "org",
      "children": [
          {
              "name": "p2_1.pdf",
              "type": "file",
              "path": "/plans/p2/p2_1.pdf",
              "tag": "org"
          },
          {
              "name": "p2_2.pdf",
              "type": "file",
              "path": "/plans/p2/p2_2.pdf",
              "tag": "org"
          },
          {
              "name": "test2",
              "type": "folder",
              "path": "/plans/p2/test2",
              "tag": "org",
              "children": [
  
              ]
          }
      ]
  },
  {
      "name": "resume.pdf",
      "type": "file",
      "path": "/plans/resume.pdf",
      "tag": "org"
  }
  ]
  </code></pre>
  
  <p>I'm currently using the os.walk() python function to go through the tree and creating lists of dicts to generate a "dumpable" list using json.dumps() but i didn't know how to do it recursively. </p>
  
  <p>Here a quick code draft:</p>
  
  <pre><code>def tree_to_json(rootdir):
  main = []
  for path, dirs, files in os.walk(rootdir):
          for curdir in dirs:
              child = []
              new_dir = {"name": curdir,
                         "type": "folder",
                         "path": path + os.sep + curdir,
                         "children": child}
              main.append(new_dir)
          for curfile in files:
              new_file = {"name": curfile,
                          "type": "file",
                          "path": path + os.sep + curfile}
              main.append(new_file)
  return json.dumps(main, sort_keys=True, indent=2, separators=(',', ': '))
  </code></pre>