Python folder names in the directory

前端 未结 8 1109
傲寒
傲寒 2020-12-23 23:15

how can i get the folder names existing in a directory using Python ?

I want to save all the subfolders into a list to work with the names after that but i dont know

相关标签:
8条回答
  • 2020-12-23 23:28

    You should import os first.

    import os
    files=[]
    files = [f for f in sorted(os.listdir(FileDirectoryPath))]
    

    This would give you list with all files in the FileDirectoryPath sorted.

    0 讨论(0)
  • 2020-12-23 23:32

    For python 3 I'm using this script

    import os
    
    root='./'
    dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]
    for dir in dirlist:
            print(dir)
    
    0 讨论(0)
  • 2020-12-23 23:38

    Starting with Python 3.4, you can also use the new pathlib module:

    from pathlib import Path
    
    p = Path('some/folder')
    subdirectories = [x for x in p.iterdir() if x.is_dir()]
    
    print(subdirectories)
    
    0 讨论(0)
  • 2020-12-23 23:39

    Use os.walk(path)

    import os
    
    path = 'C:\\'
    
    for root, directories, files in os.walk(path):
        for directory in directories:
            print os.path.join(root, directory)
    
    0 讨论(0)
  • 2020-12-23 23:41

    You can use os.walk()

    # !/usr/bin/python
    
    import os
    
    directory_list = list()
    for root, dirs, files in os.walk("/path/to/your/dir", topdown=False):
        for name in dirs:
            directory_list.append(os.path.join(root, name))
    
    print directory_list
    

    EDIT

    If you only want the first level and not actually "walk" through the subdirectories, it is even less code:

    import os
    
    root, dirs, files = os.walk("/path/to/your/dir").next()
    print dirs
    

    This is not really what os.walk is made for. If you really only want one level of subdirectories, you can also use os.listdir() like Yannik Ammann suggested:

    root='/path/to/my/dir'
    dirlist = [ item for item in os.listdir(root) if os.path.isdir(os.path.join(root, item)) ]
    print dirlist
    
    0 讨论(0)
  • 2020-12-23 23:41

    I use os.listdir

    Get all folder names of a directory

    folder_names = []
    for entry_name in os.listdir(MYDIR):
        entry_path = os.path.join(MYDIR, entry_name)
        if os.path.isdir(entry_path):
            folder_names.append(entry_name)
    

    Get all folder paths of a directory

    folder_paths = []
    for entry_name in os.listdir(MYDIR):
        entry_path = os.path.join(MYDIR, entry_name)
        if os.path.isdir(entry_path):
            folder_paths.append(entry_path)
    

    Get all file names of a directory

    file_names = []
    for file_name in os.listdir(MYDIR):
        file_path = os.path.join(MYDIR, file_name)
        if os.path.isfile(file_path):
            file_names.append(file_name)
    

    Get all file paths of a directory

    file_paths = []
    for file_name in os.listdir(MYDIR):
        file_path = os.path.join(MYDIR, file_name)
        if os.path.isfile(file_path):
            file_paths.append(file_path)
    
    0 讨论(0)
提交回复
热议问题