Python: 复制文件组到子目录

Python: copying files group to subdirectories

我正在使用 Python,我想复制某个文件夹的 3 个文件 /Users/jake/Desktop/exp到属于目录/toyspace的其他子目录的所有子文件夹:

/Users/jake/Desktop/toyspace/1A/AAA
/Users/jake/Desktop/toyspace/1A/BBB
/Users/jake/Desktop/toyspace/1A/CCC

/Users/jake/Desktop/toyspace/2B/AAA
/Users/jake/Desktop/toyspace/2B/BBB
/Users/jake/Desktop/toyspace/2B/CCC

因此所有子目录的子文件夹名称都相同。我写了类似的东西:

from distutils.dir_util import copy_tree

def myfunc (source, destination):

    fromDirectory = source
    toDirectory = destination

    copy_tree(fromDirectory, toDirectory)

    for subfold in toDirectory:
        myfunc(fromDirectory, subfold)

其中来源=/Users/jake/Desktop/exp目的地=/Users/jake/Desktop/toyspace,但它returns我一个错误:

DistutilsFileError: could not create '/motif_list.txt': Read-only file system

你能帮帮我吗?提前致谢!

很遗憾,我没有使用过 distutils,但您可以尝试使用下面的 os 命令实现自动化

import os
def copy_folders(source_dir, destination_dir):
    files = os.listdir(source_dir)
    for sub_folder1 in os.listdir(destination_dir):
        for sub_folder in os.listdir(destination_dir + sub_folder1 + '/'):
            for file in files:
                os.system('cp ' + source_dir + file + ' ' + destination_dir + sub_folder1 + '/' + sub_folder + '/')

让我知道进展如何:)