开发者

Obtaining a unique top-down list of folders in python

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜