如何获取子目录名称列表 [英] How to get list of subdirectories names

查看:68
本文介绍了如何获取子目录名称列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个目录,其中包含文件夹以及不同格式的文件.

There is a directory that contains folders as well as files of different formats.

import os 
my_list = os.listdir('My_directory')

将返回文件和文件夹名称的完整内容.我可以使用例如 endswith('.txt')方法仅选择文本文件名称,但是如何获取仅文件夹名称的列表?

will return full content of files and folders names. I can use, for example, endswith('.txt') method to select just text files names, but how to get list of just folders names?

推荐答案

我通常在一次组装一个列表的同时检查目录.假设有一个名为 foo 的目录,我想检查一下子目录:

I usually check for directories, while assembling a list in one go. Assuming that there is a directory called foo, that I would like to check for sub-directories:

import os
output = [dI for dI in os.listdir('foo') if os.path.isdir(os.path.join('foo',dI))]

这篇关于如何获取子目录名称列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆