简体   繁体   English

如何从基于 python 文件夹名称列表的目录中获取 select 文件夹?

[英]How to select folders from a directory based on a python list of the folder names?

I have a list of folder names - "df_train_pos_list"我有一个文件夹名称列表 - “df_train_pos_list”

I want to iterate through a directory and select folders with those names, and add them to another list - "train_images"我想遍历一个目录和 select 个具有这些名称的文件夹,并将它们添加到另一个列表 - “train_images”

So far what I have tried doesn't work:到目前为止,我尝试过的方法不起作用:

train_images = []
train_labels = []

for i in df_train_pos_list:
    for currentpath, folders, files in os.walk('D:\Arm C Deep Learning\SH_OCTAPUS\Train'):
        for file in files:
            if i in currentpath:
                train_images.append('D:\Arm C Deep Learning\SH_OCTAPUS\Train' + file)
                train_labels.append(1)
            else:
                train_images.append('D:\Arm C Deep Learning\SH_OCTAPUS\Train' + file)
                train_labels.append(0)
train_labels = np.asarray(train_labels, dtype=np.int64)
print(train_labels)
np.unique(train_labels, return_counts='TRUE')

Kind of unsure if you want to add folder path to the list or the individual files in the folder to your list but the below snippet will add the folder paths to your trains_list .有点不确定你是想将文件夹路径添加到列表还是文件夹中的单个文件到你的列表,但下面的代码片段会将文件夹路径添加到你的trains_list would need more details on what you want out of the label to add that.需要更多关于您想要从 label 中获得什么的详细信息才能添加。

import os
df_train_pos_list =[]
train_images = []
#train_labels = []
root = 'D:\Arm C Deep Learning\SH_OCTAPUS\Train'
for f in os.listdir(root):
    if f in df_train_pos_list:
        train_label = 1
    else:
        train_label = 0
    train_images.append((os.path.join(root,f),train_label)) #this will add your folder file path to train images

for folder, label in train_images:
    if label==1:
        #do something here

From what I understood, you are trying to generate 2 lists: one containing all the paths in "D:\Arm C Deep Learning\SH_OCTAPUS\Train" and one containing 0s and 1s depending on whether a path is in df_train_pos_list .据我了解,您正在尝试生成 2 个列表:一个包含 "D:\Arm C Deep Learning\SH_OCTAPUS\Train" 中的所有路径,另一个包含 0 和 1,具体取决于路径是否在df_train_pos_list中。

This should do the trick:这应该可以解决问题:

from pathlib import Path

df_train_pos_list = []
train_images = []
train_labels = []
df_train_pos_set = set(df_train_pos_list)

for path in Path("D:\Arm C Deep Learning\SH_OCTAPUS\Train").glob("*"):
    train_images.append(path)
    train_labels.append(1 if path.name in df_train_pos_set else 0)

A couple things to note:有几点需要注意:

  • pathlib is best practice when dealing with the file system. pathlib是处理文件系统的最佳实践。
  • I'm creating a set from your df_train_pos_list to improve complexity.我正在从您的df_train_pos_list创建一个set以提高复杂性。 It will take O(N) time complexity to create the set from the list but it will take O(1) to check whether a path is in the set whereas it would take O(N) using a list .list创建set需要 O(N) 时间复杂度,但检查路径是否在set中需要 O(1),而使用list需要 O(N)。

暂无
暂无

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

相关问题 Python:如何基于给定文件夹的内容创建具有名称的列表? - Python: how to create a list with names based on the content of a given folder? 如何从目录中的文件夹名称中提取后缀字符串? (蟒蛇) - How to Extract A String of the Suffix from Folder Names in a Directory? (Python) 如何根据文件夹名称将文件从一个带有子文件夹的目录复制到另一个带有子文件夹的目录? - How do I copy files from one directory with sub-folders to another directory with sub-folders based on folder name? 目录中的 Python 文件夹名称 - Python folder names in the directory 使用python将文件夹从文件夹列表移动到其他文件夹列表 - move folders from folder list to other folder list using python 如何使用 python 将几个文件夹从一个目录复制到 Linux 中的另一个文件夹 - how do I copy over a few folders from one directory into another folder in Linux using python 提取目录中的文件夹名称列表后,如何使用 Python 将它们重命名为另一种格式? - After extracting the list of folder names in a directory, how do I rename them in another format using Python? 如何扫描目录中的文件夹并获取末尾编号最高的文件夹-python - how to scan folders in directory and get the folder with the highest number at the end -python select n 基于排名列表的 python 列表中排名靠前的名称 - select n top ranked names from a python list based on a rank-list Python:识别文件夹结构中的数字名称文件夹 - Python: Identifying numerically names folders in a folder structure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM