如何获取Python中的所有直接子目录 [英] How to get all of the immediate subdirectories in Python

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

问题描述

我正在尝试编写一个简单的Python脚本,它将index.tpl复制到所有子目录中的index.html(除了一些例外)。

I'm trying to write a simple Python script that will copy a index.tpl to index.html in all of the subdirectories (with a few exceptions).

尝试获取子目录列表,我陷入困境。

I'm getting bogged down by trying to get the list of subdirectories.

推荐答案

import os
def get_immediate_subdirectories(a_dir):
    return [name for name in os.listdir(a_dir)
            if os.path.isdir(os.path.join(a_dir, name))]

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

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