【问题标题】:how to loop through certain directories in a directory structure in python?如何遍历python目录结构中的某些目录?
【发布时间】:2016-10-06 19:51:57
【问题描述】:

我有以下目录结构

正如您在图片中看到的,在不同的目录中有很多 .0 文件。此目录结构存在 36 个文件夹(Human_C1 到 C36),每个 Human_C[num] 文件夹都有一个 1_image_contours 文件夹,其中有一个包含所有相关 .0 文件的轮廓文件夹。

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

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

提前致谢

【问题讨论】:

    标签: python file loops operating-system


    【解决方案1】:

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

    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 中使用 lsecho 一样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-22
      • 2016-09-09
      • 2011-07-23
      • 1970-01-01
      相关资源
      最近更新 更多