简体   繁体   English

使用 os.walk 从每个目录中获取第一个文件

[英]Grabbing first file from each directory using os.walk

I am using the following code to extract all files (with same type) in all subdirectories in parent folder.我正在使用以下代码提取父文件夹中所有子目录中的所有文件(具有相同类型)。

dcm_files = []
for root, dirs, files in os.walk(path):
    for names in files:
        if names.endswith(".dcm"):
            dcm_files.append(os.path.join(root, names))

How do I only get the first file from each subdirectories in parent folder ?如何只从父文件夹中的每个子目录中获取第一个文件?

  1. you find all the files你找到所有的files
  2. you index the first file from that list files[0]您索引该列表files[0]的第一个文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM