Copying to multiple directories at once

124 views Asked by At

I'm using shutil to copy one file to multiple folders however I keep encountering some strange errors, here is my code:

 Z1_99 = [1,2,4,5,7,9,13,14,
            15,18,19,29,33,43,
            56,60,61,76,88,92,
            144,146,169]
    name = input_file.split(".")[0]
    for space in Z1_99:
        sg_folds = os.mkdir(name+"_"+str(space))
        shutil.copy(input_file, sg_folds)

However I encounter TypeError: coercing to Unicode: need string or buffer, NoneType found, if I change the last line to read this:

shutil.copy(input_file, "./"+str(sg_folds))

All the directories get made, as well as one called "None" but no copying of the input file occurs, can anyone help me with this? I've used shutil before but sometimes it confuses me. Thanks in advance!

1

There are 1 answers

1
loopbackbee On BEST ANSWER

You are using the return code of os.mkdir, but the function doesn't return anything, so sg_folds is always None

You probably want to do this:

sg_folds = name+"_"+str(space)
os.mkdir(sg_folds)
shutil.copy(input_file, sg_folds)