Getting a list of all subdirectories in the current directory

后端 未结 29 2397
一个人的身影
一个人的身影 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:29

    Building upon Eli Bendersky's solution, use the following example:

    import os
    test_directory = 
    for child in os.listdir(test_directory):
        test_path = os.path.join(test_directory, child)
        if os.path.isdir(test_path):
            print test_path
            # Do stuff to the directory "test_path"
    

    where is the path to the directory you want to traverse.

提交回复
热议问题