Getting a list of all subdirectories in the current directory

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

    You could just use glob.glob

    from glob import glob
    glob("/path/to/directory/*/")
    

    Don't forget the trailing / after the *.

    0 讨论(0)
  • 2020-11-22 08:29

    Although this question is answered a long time ago. I want to recommend to use the pathlib module since this is a robust way to work on Windows and Unix OS.

    So to get all paths in a specific directory including subdirectories:

    from pathlib import Path
    paths = list(Path('myhomefolder', 'folder').glob('**/*.txt'))
    
    # all sorts of operations
    file = paths[0]
    file.name
    file.stem
    file.parent
    file.suffix
    

    etc.

    0 讨论(0)
  • 2020-11-22 08:29

    This is how I do it.

        import os
        for x in os.listdir(os.getcwd()):
            if os.path.isdir(x):
                print(x)
    
    0 讨论(0)
  • 2020-11-22 08:29

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

    import os
    test_directory = <your_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 <your_directory> is the path to the directory you want to traverse.

    0 讨论(0)
  • 2020-11-22 08:29

    This function, with a given parent directory iterates over all its directories recursively and prints all the filenames which it founds inside. Too useful.

    import os
    
    def printDirectoryFiles(directory):
       for filename in os.listdir(directory):  
            full_path=os.path.join(directory, filename)
            if not os.path.isdir(full_path): 
                print( full_path + "\n")
    
    
    def checkFolders(directory):
    
        dir_list = next(os.walk(directory))[1]
    
        #print(dir_list)
    
        for dir in dir_list:           
            print(dir)
            checkFolders(directory +"/"+ dir) 
    
        printDirectoryFiles(directory)       
    
    main_dir="C:/Users/S0082448/Desktop/carpeta1"
    
    checkFolders(main_dir)
    
    
    input("Press enter to exit ;")
    
    
    0 讨论(0)
  • 2020-11-22 08:30

    Function to return a List of all subdirectories within a given file path. Will search through the entire file tree.

    import os
    
    def get_sub_directory_paths(start_directory, sub_directories):
        """
        This method iterates through all subdirectory paths of a given 
        directory to collect all directory paths.
    
        :param start_directory: The starting directory path.
        :param sub_directories: A List that all subdirectory paths will be 
            stored to.
        :return: A List of all sub-directory paths.
        """
    
        for item in os.listdir(start_directory):
            full_path = os.path.join(start_directory, item)
    
            if os.path.isdir(full_path):
                sub_directories.append(full_path)
    
                # Recursive call to search through all subdirectories.
                get_sub_directory_paths(full_path, sub_directories)
    
    return sub_directories
    
    0 讨论(0)
提交回复
热议问题