Python my recursive code to create a json file from directory tree is resulting in duplicate names for files

Viewed 26

I've put together a piece of code to create a representation of a directory tree in a JSON file from other code I found here in SO, but it's not working properly. The objective is to create a tree with folders and inside the filenames and respective md5 hash. The problem is, the code lists some filenames several times, but with different hashes. I've tried to pinpoint the error but so far unsuccessfully.

The code:

import os, sys, json
from os import listdir
from os.path import isfile, join
import hashlib

BUF_SIZE = 65536  

md5 = hashlib.md5()

def is_valid_dir(path, subdirs):
    return all(os.path.isdir(path) for subdir in subdirs)

def all_dirs_with_subdirs(path, subdirs):
    children = []
    filemd5 = []
    for name in os.listdir(path):
        subpath = os.path.join(path, name)
        if os.path.isdir(subpath) and is_valid_dir(subpath, subdirs):
            for file in listdir(subpath):
                if isfile(join(subpath, file)):
                    with open(join(subpath, file), 'rb') as f:
                        while True:
                            data = f.read(BUF_SIZE)
                            if not data:
                                break
                            md5.update(data)
                            filemd5.append([file, md5.hexdigest()])
            children.append({
                'text': name,
                'type': 'directory',
                'children': all_dirs_with_subdirs(subpath, subdirs),
                'files': filemd5
            })


    return children

def get_directory_listing(path):
    try:
        output = {}
        output["text"] = path
        output["type"] = "directory"
        output["children"] = all_dirs_with_subdirs("c:\sistema", ('Maps', 'Temp'))
        return output

    except WindowsError:
        pass

listing = get_directory_listing("C:\sistema")
print(json.dumps(listing, indent=4))
0 Answers
Related