2

I am trying to store directory structure in a nested dictionary. The tree of the directory

├── dirA
│   ├── dirB1
│   │   └── file1.txt
│   └── dirB2
│       └── file2.txt
├── templates
│   ├── base.html
│   └── report.html
└── test.py  

The nested dictionary is like:

{'dirs': {'.': {'dirs': {'dirA': {'dirs': {'dirB1': {'dirs': {},
                                                     'files': ['file1.txt']}, 
                                           'dirB2': {'dirs': {},
                                                     'files':['file2.txt']}
                                                     }                                          
                                  'files': []}, 
                         'templates':{'dirs':{},
                                  'files':['base.html', 'report.html']}},
         'files': ['test.py']}},
 'files': []}

I think recursion is a good way to do this.

import os                                                 
import pprint              

pp = pprint.PrettyPrinter()
def path_to_dict(path): 
    d = {'dirs':{},'files':[]}
    name = os.path.basename(path)
    if os.path.isdir(path):
        if name not in d['dirs']:
            d['dirs'][name] = {'dirs':{},'files':[]}
        for x in os.listdir(path):
            d['dirs'][name]= path_to_dict(os.path.join(path,x))                                                 
    else:                  
        d['files'].append(name)        
    return d               

mydict = path_to_dict('.')
pp.pprint(mydict)

The result is different from what I expected. But I don't know which step goes wrong in the recursion.

3
  • I know why this code output the wrong result. d = {'dirs':{},'files':[]} will reset the dictionary items. Commented Sep 25, 2017 at 9:01
  • 1
    Why do you need this? os.walk() is usually much easier to work with when traversing directory structures. Commented Sep 25, 2017 at 9:33
  • @MartinEvans Yes, os.walk() can go through the directory. But I am trying to stroe the directory structure. Commented Sep 26, 2017 at 2:04

1 Answer 1

5

You are creating the dict object on every call, what you need to do is pass its d['dirs'][name] value on every call to allow its recursive construction:

import os
import pprint

pp = pprint.PrettyPrinter()

def path_to_dict(path, d):

    name = os.path.basename(path)

    if os.path.isdir(path):
        if name not in d['dirs']:
            d['dirs'][name] = {'dirs':{},'files':[]}
        for x in os.listdir(path):
            path_to_dict(os.path.join(path,x), d['dirs'][name])
    else:
        d['files'].append(name)
    return d


mydict = path_to_dict('.', d = {'dirs':{},'files':[]})

pp.pprint(mydict)
Sign up to request clarification or add additional context in comments.

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.