You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
873 B

11 months ago
import os
import shutil
import pandas as pd
# 读取包含索引和标签的数据文件
df = pd.read_csv('data/data.csv', encoding='GBK')
# 设置原始文件夹路径和目标文件夹路径
source_folder = 'data/train' # 原始文件夹路径
target_folder = 'data_classification/train' # 目标文件夹路径
# 遍历 DataFrame
for index, row in df.iterrows():
file_index = row['索引'] # 索引对应的列名
file_label = row['标签'] # 标签对应的列名
source_file = os.path.join(source_folder, f'{file_index}.jpg') # 原始文件路径
target_folder_label = os.path.join(target_folder, str(file_label)) # 目标文件夹路径
# 创建对应标签的目标文件夹(如果不存在)
os.makedirs(target_folder_label, exist_ok=True)
# 复制文件到目标文件夹
shutil.copy(source_file, target_folder_label)