Getting a list of all subdirectories in the current directory

后端 未结 29 2389
一个人的身影
一个人的身影 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-22 08:33

    The easiest way:

    from pathlib import Path
    from glob import glob
    
    current_dir = Path.cwd()
    all_sub_dir_paths = glob(str(current_dir) + '/*/') # returns list of sub directory paths
    
    all_sub_dir_names = [Path(sub_dir).name for sub_dir in all_sub_dir_paths] 
    

提交回复
热议问题