简体   繁体   中英

How can I create a folder tree in json format using python?

I am wondering, if it possible, to provide a folder path and let a python script scan the given folder and return a json tree with the amount of files for each folder. The tree should contain every sub-folder:

Eg result:

[{
  foldername: "folder1",
  amount_of_files: 123,
  children: [
    {
      foldername: "folder1.1",
      amount_of_files: 3,
      children: []
    },
    {
      foldername: "folder1.2",
      amount_of_files: 5,
      children: [
        {
          foldername: "folder1.2.1",
          amount_of_files: 20,
          children: []
        }
      ]
    }
  ]
},
{
  foldername: "folder2",
  amount_of_files: 1,
  children: [
    {
      foldername: "folder2.1",
      amount_of_files: 3,
      children: [
        {
          foldername: "folder2.1.1",
          amount_of_files: 2,
          children: [
            {
              foldername: "folder2.1.1.1",
              amount_of_files: 24,
              children: []
            }
          ]
        }
      ]
    },
    {
      foldername: "folder1.2",
      amount_of_files: 5,
      children: []
    }
  ]
}
]

You can use os.listdir with recursion:

import os, json
def get_tree(path=os.getcwd()):
   return {'foldername':path, 
           'amount_of_files':sum(not os.path.isdir(os.path.join(path, k)) for k in os.listdir(path)),
           'children':[get_tree(os.path.join(path, k)) for k in os.listdir(path) if os.path.isdir(os.path.join(path, k))]}


with open('folder_tree.json', 'w') as f:
   json.dump(get_tree(), f)

To produce a list of dictionaries, with each dictionary containing the folder name and number of files, you can use a recursive generator function:

def get_tree(path=os.getcwd()):
   yield {'foldername':path, 'amount_of_files':sum(not os.path.isdir(os.path.join(path, k)) for k in os.listdir(path))}
   for i in os.listdir(path):
      if os.path.isdir(os.path.join(path, i)):
         yield from get_tree(os.path.join(path, i))

with open('folder_tree.json', 'w') as f:
   json.dump(list(get_tree()), f)

This seems to do the job:

from os import listdir
from os.path import isdir, isfile, basename, join
from json import dumps

def folder(d):
    result = dict(
            amount_of_files=0,
            foldername=basename(d)
            )
    for each in listdir(d):
        path = join(d, each)
        if isdir(path):
            if 'children' not in result:
                result['children'] = list()
            result['children'].append(folder(path))
        if isfile(path):
            result['amount_of_files'] += 1
    return result

print(dumps(folder('.')))    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM