Python: os.walk 指定目录和文件

Python: os.walk to specific directory and file

我的文件结构如下所示:

|num_1

|----|dir1

|--------|dir2

|------------|dcm

|----------------\file_1

|----------------\file_2

|----------------\file_n

|num_2

|----|dir1

|--------|dcm

|------------\file_1

|------------\file_n

|num_n

我想让我们 os.walk(或更合适的东西?)遍历树,直到找到目录 "dcm"。 dcm 可以处于树的不同级别

这就是我的。谢谢!

import dicom
import re
import os

dcm = []
PATH = "C:\foo"

#find the directory we want to get to, save path
for path, dirs in os.walk(PATH): 
    for dirname in dirs:
        fullpath = os.path.join(path,dirname)
        if "dcm" in dirname:
            #copied this first_file line - just want a fast and easy way to grab ONE file in the dcm directory
            #without reading any of the others (for time reasons)
            first_file = next((join(path, f) for f in os.listdir(path) if isfile(join(path, f))),"none")
            fullpath = os.path.join(fullpath,first_file)
            dcm.append(fullpath)

我继续 "lazy" 方式,使用 listdir 读出 dcm 目录下的所有文件 - 确定资源成本并不太高。 话虽这么说,我认为从目录中拉出一个随机文件而不读取所有这些文件是一个有趣的查询,有人 Python 比我更应该回答!

作为参考,我的最终解决方案是...请原谅迭代器使用效率低下!我是新手,需要一个快速解决方案

for path, dirs, filename in os.walk(rootDir): #omit files, loop through later
    for dirname in dirs:
        fullpath = os.path.join(path,dirname)
        if "dcm" in dirname:
            dcm.append(fullpath)

final = []
uni = 0
final.append(dcm[0])
for i in range(len(dcm)):
    if len(os.listdir(dcm[i])) < 10:
        pass
    elif dcm[i][16:19] != final[uni][16:19]:
        final.append(dcm[i])
        uni += 1


tags = ((0x8, 0x70)),((0x8, 0x1090)), ((0x18, 0x1020))
values = []
printout = []
for p in range(len(final)):
    file = os.path.join(final[p],os.listdir(final[p])[0])
    ds = dicom.read_file(file)
    printout.append([final[p]])
    for k in range(len(tags)):
        printout.append([ds[tags[k]]])