【问题标题】:Python: os.walk to specific directory and filePython:os.walk 到特定目录和文件
【发布时间】:2017-08-23 08:13:50
【问题描述】:

我的文件结构如下:

|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)

【问题讨论】:

    标签: python file directory os.walk


    【解决方案1】:

    我继续使用“懒惰”的方式并使用 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]]])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-05
      • 2018-09-10
      相关资源
      最近更新 更多