首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

Python 递归遍历索引结构

2012-10-07 
Python 递归遍历目录结构Python 递归遍历目录结构?Author:lvmingyanEmail:lvmingyan86@yeah.net???def lis

Python 递归遍历目录结构

Python 递归遍历目录结构

?

Author:lvmingyan

Email:lvmingyan86@yeah.net

?

?

?

def list_dir(path, res):    '''        res = {'dir':'root', 'child_dirs' : [] , 'files' : []}        print list_dir('/root', res)    '''    for i in os.listdir(path):        temp_dir = os.path.join(path,i)        if os.path.isdir(temp_dir):            temp = {'dir':temp_dir, 'child_dirs' : [] , 'files' : []}            res['child_dirs'].append(list_dir(temp_dir, temp))        else:            res['files'].append(i)    return resdef get_config_dirs():    res = {'dir':'root', 'child_dirs' : [] , 'files' : []}    return list_dir('d:/root',res)
print get_config_dirs()
>>
{    'files': [],     'child_dirs':         [            {                'files': [],                 'child_dirs':                     [                        {                            'files': ['aa.txt'],                             'child_dirs':                                 [                                    {                                        'files': [],                                         'child_dirs': [],                                         'dir': 'd:/root\\a\\aa\\aaa'                                    }                                ],                             'dir': 'd:/root\\a\\aa'                        }                    ],                 'dir': 'd:/root\\a'},             {                'files': [],                 'child_dirs': [],                 'dir': 'd:/root\\b'            }        ],     'dir': 'd:/root'}

?

?

?

?

?

?

?

热点排行