如何提取子文件夹路径?

2024-05-15 11:03:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有大约100个文件夹和每个文件夹中的文件,应该阅读和分析。你知道吗

我可以从它们的子文件夹中读取文件,但我想从第10个文件夹开始处理,直到最后。我需要确切的文件夹路径。你知道吗

我该怎么做?你知道吗

为了澄清我的问题,我从代码中提取了一个示例:

rootDir = 'D:/PhD/result/Pyradiomic_input/'
for (path, subdirs, files) in os.walk(rootDir):        
    sizefile=len(path)
    if "TCGA-" in path :
        print(path)

输出为:

D:/PhD/result/Pyradiomic_input/TCGA-02-0006
D:/PhD/result/Pyradiomic_input/TCGA-02-0009
D:/PhD/result/Pyradiomic_input/TCGA-02-0011
D:/PhD/result/Pyradiomic_input/TCGA-02-0027
D:/PhD/result/Pyradiomic_input/TCGA-02-0046
D:/PhD/result/Pyradiomic_input/TCGA-02-0069

现在我的问题是,我如何从例如D:/PhD/result/Pyradiomic_input/TCGA-02-0046开始工作到最后,而不是从顶部开始?我试过一些主意,但都不管用。你知道吗


Tags: 文件path代码in路径文件夹示例for
2条回答

你可以跳过你不感兴趣的值。这里有点简化:

counter = 0

# mocking the file operations
for path in ["/dir-1", "/dir-2", "/dir-3", "/dir-4", "/dir-5"]:
  # skip the first two paths
  if counter < 2:
    counter += 1
    continue

  # do something    
  print(path)

或者可以先收集路径,如下所示:

paths = []

# mocking the file operations
for path in ["/dir-1", "/dir-2", "/dir-3", "/dir-4", "/dir-5"]:
  # collect paths in array
  paths.append(path)

# skip the first two elements
paths = paths[2:]

for path in paths:
  # do something
  print(path)

如果使用生成器表达式,第二个版本可能会变短一些,但我倾向于可读性。你知道吗

您可以设置一个标记,以便在命中特定目录时捕获

rootDir = 'D:/PhD/result/Pyradiomic_input/'
first_folder = 'TCGA-02-0046'

process = False
for (path, subdirs, files) in os.walk(rootDir):        
    sizefile=len(path)
    if "TCGA-" in path :
        print(path)

        if first_folder in path:
            process = True

        if process:
            #process folder

如果希望特定文件夹指示脚本应停止处理

rootDir = 'D:/PhD/result/Pyradiomic_input/'
first_folder = 'TCGA-02-0046'
last_folder = 'TCGA-02-0099'

process = False
for (path, subdirs, files) in os.walk(rootDir):        
    sizefile=len(path)
    if "TCGA-" in path :
        print(path)

        if first_folder in path:
            process = True

        if last_folder in path:
            break

        if process:
            #process folder

还可以设置要处理的目录列表

rootDir = 'D:/PhD/result/Pyradiomic_input/'
process_dirs = ['TCGA-02-0046', ...]

process = False
for (path, subdirs, files) in os.walk(rootDir):        
    sizefile=len(path)
    if "TCGA-" in path :
        print(path)

        if any(d in path for d in process_dirs):
            #process folder

相关问题 更多 >