开发者

Is there a one-liner to list a directory two levels deep where the second level is an only-child, but not known?

Suppose you have a directory structure like this:

A/
   B/
      a.1
      b.2
      c.3

I'm wondering if there's a way, knowing that B has no siblings AND NOT KNOWING B's NAME, to do an os.listdir operation in one swoop (that is, without calling os.listdir twice), instead of in three commands like so:

root =开发者_运维技巧 "A"
secondLevel = os.listdir(root)[0]
listing = os.listdir(os.path.join(root,secondLevel))


import glob
os.listdir(glob.glob('A/*')[0])

or maybe even

glob.glob('A/*/*')


You are looking for os.walk.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜