Python 打开包含在其他文件夹中的文件夹

Python 打开包含在其他文件夹中的文件夹,python,directory,Python,Directory,我有一系列的子文件夹包含在主文件夹中。我想访问每个子文件夹,以便使用其中的文件。我尝试了以下方法,但显然我的“追加”方法在Python上是不可能的。有人能帮我吗 import os os.chdir('C\\current folder') for subfolder in os.listdir(os.getcwd()): os.chdir('C\\current folder\\'subfolder'') 这段代码对我帮助很大。实际上是从这里取来的……但它是一个救命恩人 这里是一个示

我有一系列的子文件夹包含在主文件夹中。我想访问每个子文件夹,以便使用其中的文件。我尝试了以下方法,但显然我的“追加”方法在Python上是不可能的。有人能帮我吗

import os
os.chdir('C\\current folder')
for subfolder in os.listdir(os.getcwd()):
    os.chdir('C\\current folder\\'subfolder'')
这段代码对我帮助很大。实际上是从这里取来的……但它是一个救命恩人


这里是一个示例,我在
路径到我的根目录下递归搜索所有目录中的
.png
文件,并将其附加到名为
fis
的列表中的所有文件和路径的列表中

import os
fis=[]
rootdir=os.walk('path_to_my_root_directory`)
for root,subd,fils in rootdir :
    for name in fils:
        if name.endswith('.png'):
            fis.append(os.path.join(root,name))
os.walk()
import os
fis=[]
rootdir=os.walk('path_to_my_root_directory`)
for root,subd,fils in rootdir :
    for name in fils:
        if name.endswith('.png'):
            fis.append(os.path.join(root,name))