本文介绍了使用os.walk以便递归到Python中的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我将以下代码作为Python文件的一部分,它遍历名为controllers的文件夹中的.py文件,并对它们执行一些操作.

I have the following code as part of a Python file, and it traverses .py files in the folder called controllers, and preforms some operations with them.

这是我的原型,但现在我想使用 os.walk 进行递归进入文件夹.

This is my prototype, but now I want to use os.walk to recurse into the folders.

controller_folder_path = "applications/%s/controllers/*.py" % application_name
for module_path in glob.glob(controller_folder_path):
    print module_path

有帮助吗?

推荐答案

import os

controller_folder_path = "applications/%s/controllers" % application_name
for root, dirs, files in os.walk(controller_folder_path):
    for module_path in files:
        module_path = os.path.join(root, module_path)
        if module_path.endswith('.py'):
            print module_path

这篇关于使用os.walk以便递归到Python中的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 17:35