简体   繁体   中英

how to get a list of all the folders in current directory

How to get a list of all the folders in current directory. I know that we can get list of files and folders with os.walk() but I do not want to do the extra work as it is just unnecessary in my case.

[f for f in os.listdir('.') if os.path.isdir(f)]
print [fname for fname in os.listdir(".") if os.path.isdir(fname)]

The dirs in each directory are the second item in the tuple returned by os.walk, in each interation. So,one can simply do:

dirs = os.walk(".").next()[1]

No need to iterate over the remaining of os.walk.

from os import listdir
from os.path import isdir

path = '.'
dirs = filter(isdir, listdir(path))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM