Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中,如何循环遍历目录结构中的某些目录?_Python_File_Loops_Operating System - Fatal编程技术网

在python中,如何循环遍历目录结构中的某些目录?

在python中,如何循环遍历目录结构中的某些目录?,python,file,loops,operating-system,Python,File,Loops,Operating System,我有以下目录结构 正如您在图片中看到的,在不同的目录中有许多.0文件。此目录结构适用于36个文件夹(Human_C1至C36),每个Human_C[num]文件夹有一个1_image_contours文件夹,该文件夹有一个contours文件夹,其中包含所有相关的.0文件 这些.0文件包含一些坐标(x,y)。我希望遍历所有这些文件,获取其中的数据并将其放入excel表中(我正在使用pandas) 问题是,我如何只遍历这些文件集而不遍历其他文件?(contour_图像文件夹中也可以有.0文件)

我有以下目录结构

正如您在图片中看到的,在不同的目录中有许多.0文件。此目录结构适用于36个文件夹(Human_C1至C36),每个Human_C[num]文件夹有一个1_image_contours文件夹,该文件夹有一个contours文件夹,其中包含所有相关的.0文件

这些.0文件包含一些坐标(x,y)。我希望遍历所有这些文件,获取其中的数据并将其放入excel表中(我正在使用pandas)

问题是,我如何只遍历这些文件集而不遍历其他文件?(contour_图像文件夹中也可以有.0文件)


提前感谢

因为您的结构不是递归的,所以我建议您:

import glob
zero_files_list = glob.glob("spinux/generated/Human_C*/*/contours/*.0")
for f in zero_files_list:
    print("do something with "+f)
spinux
的父目录运行它,否则您将没有匹配项


它将扩展上面固定目录树的模式,就像您在linux shell中使用
ls
echo
一样。

由于您的结构不是递归的,我建议您:

import glob
zero_files_list = glob.glob("spinux/generated/Human_C*/*/contours/*.0")
for f in zero_files_list:
    print("do something with "+f)
spinux
的父目录运行它,否则您将没有匹配项

它将扩展上面固定目录树的模式,就像在linux shell中使用
ls
echo
一样