for 循环 shutil.copytree 错误地复制每个文件夹的所有文件

for loop with shutil.copytree wrongly copying ALL files for EVERY single folder

在这个 for 循环中,它复制每个子文件夹的所有文件夹。所以它花了很长时间,而不是我想要的。所以例如对于文件夹“SPRING2158”,它正在复制从“SPRING00001”到“SPRING09999”结尾的所有内容。但它只应复制以“SPRING2158”开头的图像。不适合所有。

import os
import shutil 

path = os.path.expanduser('/content/images/FemaleImages')   
for file_name in os.listdir(path):
  if file_name[:10] in nameTrainF:
    shutil.copytree(path+'/', '/content/OutTrainF/'+file_name[:10]+'/')   
  if file_name[:10] in nameValF:
    shutil.copytree(path+'/', '/content/OutValF/'+file_name[:10]+'/')   
  if file_name[:10] in nameTestF:
    shutil.copytree(path+'/', '/content/OutTestF/'+file_name[:10]+'/')   

为了让您了解数据结构,下面是从 RAR 格式提取数据时数据的样子。

 !unrar e "/content/drive/My Drive/femaleset.rar" "/content/images/FemaleImages/" 

Extracting  /content/images/FemaleImages/SPRING4796-D7-V40-H50.png    OK 
Extracting  /content/images/FemaleImages/SPRING4796-D7-V40-H60.png    OK 
Extracting  /content/images/FemaleImages/SPRING4796-D7-V40-H70.png    OK 
Extracting  /content/images/FemaleImages/SPRING4796-D7-V40-H80.png    OK 
Extracting  /content/images/FemaleImages/SPRING4796-D7-V40-H90.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H0.png      OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H10.png     OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H100.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H110.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H120.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H130.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H140.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H150.png    OK 
Extracting  /content/images/FemaleImages/SPRING4798-D7-V0-H160.png    OK 

Colab 中输出文件夹的屏幕截图

为评论中的其他问题添加了下面的更新代码:

path = os.path.expanduser('/content/images/FemaleImages')  
for file_name in os.listdir(path):
  if file_name[:10] in nameTrainF:
    shutil.copy2(os.path.join(path, file_name), os.path.join('/content/OutTrainF/', file_name[:10],'/'))   
  if file_name[:10] in nameValF:
    shutil.copy2(os.path.join(path, file_name), os.path.join('/content/OutValF/', file_name[:10],'/')) 
  if file_name[:10] in nameTestF:
    shutil.copy2(os.path.join(path, file_name), os.path.join('/content/OutTestF/', file_name[:10],'/'))

您不只是复制迭代的当前文件,您每次都在复制 path 中的所有内容。使用 shutil.copy2() 复制文件,而不是 shutil.copytree()

此外,您应该使用 os.path.join() 来组合目录和文件名,而不是串联。

for file_name in os.listdir(path):
  prefix = name[:10]
  if prefix in nameTrainF:
    target = os.path.join('/content/OutTrainF/', prefix, '')
    os.makedirs(target, exist_ok=True)
    shutil.copy2(os.path.join(path, file_name), target)   
  if prefix in nameValF:
    target = os.path.join('/content/OutValF/', prefix, '')
    os.makedirs(target, exist_ok=True)
    shutil.copy2(os.path.join(path, file_name), target)   
  if prefix in nameTestF:
    target = os.path.join('/content/OutTestF/', prefix, '')
    os.makedirs(target, exist_ok=True)
    shutil.copy2(os.path.join(path, file_name), target)