开发者

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

开发者 https://www.devze.com 2023-02-07 07:19 出处:网络
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

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

精彩评论

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