Getting a list of all subdirectories in the current directory

后端 未结 29 2736
一个人的身影
一个人的身影 2020-11-22 08:02

Is there a way to return a list of all the subdirectories in the current directory in Python?

I know you can do this with files, but I need to get the list of direct

29条回答
  •  日久生厌
    2020-11-22 08:39

    Here are a couple of simple functions based on @Blair Conrad's example -

    import os
    
    def get_subdirs(dir):
        "Get a list of immediate subdirectories"
        return next(os.walk(dir))[1]
    
    def get_subfiles(dir):
        "Get a list of immediate subfiles"
        return next(os.walk(dir))[2]
    

提交回复
热议问题