从目录结构构建字典 [英] Building a dictionary from directory structure

查看:33
本文介绍了从目录结构构建字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个看起来像这样的字典:

I'm trying to build a dictionary that looks like this:

nodes = {
    'var': {
        'type': 'd',
        'full_path': '/var'
        'active': True
        'www': {
            'type': 'd',
            'full_path': '/var/www',
            'active': True
            'index.html': {
                'type': 'f',
                'full_path': '/var/www/index.html',
                'active': False
            }
        'log': {
            'type': 'd',
            'full_path': '/var/log',
            'active': False
        }
    }
    'srv': {
        'type': 'd',
        'full_path': '/srv',
        'active': True
    }
}

我需要它由两部分构建...第一个需要来自所有活动"的文件系统.第二个需要来自文件的完整路径列表,其中所有内容都处于非活动状态.

I need it to be built by two pieces... The first needs to be from the file system where everything is 'active'. The second needs to come from a listing of full paths of files where everything is inactive.

所以...

nodes = {}
for f, d, r in os.walk(root_path):
    # append active items to nodes
for f in os.system(command_that_gets_files)
    # append inactive items to nodes; not overwriting active

我确定我遗漏了细节......

I'm sure I'm missing details...

推荐答案

这是获取活动文件的一种方法.我发现递归比使用 os.walk() 的迭代数据更容易.如果您需要保留比文件类型更多的信息,您可以取消注释 result['stat'] 行.

Here's one way to get the active files. I found it easier to recurse than to use os.walk()'s iterative data. You may uncomment the result['stat'] line if you need to preserve more information than file type.

每个文件都有一个字典条目,如:

Every file has a dict entry like:

filename : { 'active' : True,
             'full_path' = '/path/to/filename',
             'type' : 'f' }

每个目录都有一个字典条目,如:

Every directory has a dict entry like:

dirname : { 'active' : True,
            'full_path' = '/path/to/dirname',
             'type' : 'd',
             items = { 'itemname' : {...}, ... } }

给你:

import sys
import os
from stat import *
import pprint

def PathToDict(path):
    st = os.stat(path)
    result = {}
    result['active'] = True
    #result['stat'] = st
    result['full_path'] = path
    if S_ISDIR(st.st_mode):
        result['type'] = 'd'
        result['items'] = {
            name : PathToDict(path+'/'+name)
            for name in os.listdir(path)}
    else:
        result['type'] = 'f'
    return result


pprint.pprint(PathToDict(sys.argv[1]))

结果:

{'active': True,
 'full_path': '/tmp/x',
 'items': {'var': {'active': True,
                   'full_path': '/tmp/x/var',
                   'items': {'log': {'active': True,
                                     'full_path': '/tmp/x/var/log',
                                     'items': {},
                                     'type': 'd'},
                             'www': {'active': True,
                                     'full_path': '/tmp/x/var/www',
                                     'items': {'index.html': {'active': True,
                                                              'full_path': '/tmp/x/var/www/index.html',
                                                              'type': 'f'}},
                                     'type': 'd'}},
                   'type': 'd'}},
 'type': 'd'}

这篇关于从目录结构构建字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆