开发者

Obtaining a unique top-down list of folders in python

开发者 https://www.devze.com 2023-02-20 03:06 出处:网络
Given these folders s:\\test\\zoneA\\UnitA s:\\test\\zoneB\\UnitA s:\\test\\zoneB\\UnitB\\Item_1 I would like to pass to a 开发者_如何学JAVAfunction \'s:\\test\' and get unique bottom list of folde

Given these folders

s:\test\zoneA\UnitA
s:\test\zoneB\UnitA
s:\test\zoneB\UnitB\Item_1

I would like to pass to a 开发者_如何学JAVAfunction 's:\test' and get unique bottom list of folders

Resulting in what you see above... 3 entries. Instead, when I use my function

import os
for top, dirs, files in os.walk('S:\\\test'):
    for nm in dirs:
        print os.path.join(top, nm)

I get...

S:\test\zoneA
S:\test\zoneB
S:\test\zoneA\UnitA
S:\test\zoneB\UnitA
S:\test\zoneB\UnitB
S:\test\zoneB\UnitB\Item_1

Is there a way to get the bottom folder list ? Thanks much.


I guess what you want are folders without subfolders?

import os
for path, dirs, files in os.walk('S:\\\test'):
    if not dirs:
        print path
0

精彩评论

暂无评论...
验证码 换一张
取 消