【问题标题】:How to copy folders with same names recursively to another folder in Google Colab如何将具有相同名称的文件夹递归复制到 Google Colab 中的另一个文件夹
【发布时间】:2020-10-30 02:15:24
【问题描述】:
  • 我有 10 个折叠,每个折叠有 2 个文件夹 facebackground,我想在火车内复制不同的折叠,验证文件夹,以便火车或验证将有来自不同的 facebackground折叠。

  • 我尝试了以下代码,但是由于Google Colab使用python 3.6.9,我不能忽略文件夹存在的错误(如python 3.9) 所以我得到以下错误:

错误:

try:
     mkdir(name, mode)
except OSError:
     # Cannot rely on checking for EEXIST, since the operating system

FileExistsError: [Errno 17] File exists: 'train/'

我的代码:

#--------------
# Split Dataset
#==============
# we Already have the test set
# !mkdir train
# !mkdir valid
import os, shutil

fileList = os.listdir("NewDataset")
fileList.sort()
for i in [x for x in range(10) if ((x != 9) and (x != 1))]:
    # print(fileList[i])
    # Train
    if(i < 7):
        subPathList = glob.glob('NewDataset/'+fileList[i]+'/**/', recursive=False)
        for subPath in subPathList:
            shutil.copytree(subPath, 'train/')

    # Validate
    else:
        subPathList = glob.glob('NewDataset/'+fileList[i]+'/**/', recursive=False)
        for subPath in subPathList:
            shutil.copytree(subPath, 'valid/')

【问题讨论】:

    标签: python google-colaboratory shutil copytree


    【解决方案1】:

    解决方案是递归地一个一个地复制文件:

    import os, shutil
    
    for i in [x for x in range(10) if ((x != 9) and (x != 1))]:
        # print(fileList[i])
        # Train
        if(i > 2):
            subPathList = glob.glob('/content/NewDataset/'+fileList[i]+'/**/', recursive=False)
            for subPath in subPathList:
                for im in os.listdir(subPath):
                    imFullPath = os.path.join(subPath, im)
                    targetPath = os.path.join('/content/train',subPath.split('/')[-2]+ '/')
                    # print('Train: ', targetPath)
                    shutil.copy(imFullPath, targetPath)
        # Validate
        else:
            subPathList = glob.glob('/content/NewDataset/'+fileList[i]+'/**/', recursive=False)
            for subPath in subPathList:
                for im in os.listdir(subPath):
                    imFullPath = os.path.join(subPath, im)
                    targetPath = os.path.join('/content/valid',subPath.split('/')[-2] + '/')
                    # print('Validate: ', targetPath)
                    shutil.copy(imFullPath, targetPath)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 2022-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多