Use os.listdir to show directories only

眉间皱痕 提交于 2019-11-28 12:19:20

that's expected, since os.listdir only returns the names of the files/dirs, so objects are not found, unless you're running it in the current directory.

You have to join to scanned directory to compute the full path for it to work:

for filename in os.listdir(file_to_search):
    if os.path.isdir(os.path.join(file_to_search,filename)):
        dirlist.append(filename)

note the list comprehension version:

dirlist = [filename for filename in os.listdir(file_to_search) if os.path.isdir(os.path.join(file_to_search,filename))]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!