2

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:

E.g. 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: []
    }
  ]
}
]
2
  • Have you tried anything yet? If so, please share! If not, maybe make an attempt. Commented Mar 6, 2021 at 17:16
  • I know you have an answer, but would you be open to an answer using pandas ? Commented Mar 6, 2021 at 22:42

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

5 Comments

That looks awesome. How can I safe this result to a file?
This is working great. Just as a second option, is there a way on having it in an one dimensional output, giving me a 1d list of pathnames and amount of children (folder) and files?
@T.Karter Definitely, please see my recent edit
yield from get_tree(os.path.join(path, i)) gives me invalid syntax
Sorry, I need to run it with python3. Then it is working. Thank you! The only thing, the second code is missing the amount_of_children as a amount of folders an that directory.
0

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('.')))    

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.