Getting a list of all subdirectories in the current directory

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

    Since I stumbled upon this problem using Python 3.4 and Windows UNC paths, here's a variant for this environment:

    from pathlib import WindowsPath
    
    def SubDirPath (d):
        return [f for f in d.iterdir() if f.is_dir()]
    
    subdirs = SubDirPath(WindowsPath(r'\\file01.acme.local\home$'))
    print(subdirs)
    

    Pathlib is new in Python 3.4 and makes working with paths under different OSes much easier: https://docs.python.org/3.4/library/pathlib.html

提交回复
热议问题