Skip to content
Advertisement

how do I copy over a few folders from one directory into another folder in Linux using python

I’m trying to copy over a bunch of folders, read from a txt file into another folder . How do I do this? I’m using python in Linux

e.g this txt file has the following folder names

001YG  
00HFP  
00MFE  
00N38  
00NN7  
00SL4  
00T1E  
00T4B  
00X3U  
00YZL  
00ZCA  
01K8X  
01KM1  
01KML  
01O27  
01THT  
01ZWG  

and I want to copy these folders and their contents to a folder called duplicate-folder

How do I do this? I tried the following code but it gave an error because I think it’s only for files, not folders :)

with open("missing-list-files.txt",'w') as outfile:
    # outfile.write('n'.join(temp3))
    for x in temp3:
        outfile.write(x + 'n')

    filelistset = temp3     
    dest = 'mydirectoryhere'
    for file in filelistset:
        shutil.copyfile(file ,dest + file)

Advertisement

Answer

use shutil.copytree(src, dest, ...) (https://docs.python.org/3/library/shutil.html#shutil.copytree) for directories (also copies subfiles and -directories. And also don’t forget to give it the full path for file and dest (don’t know which value your temp3 var holds). If you don’t give it a full path, but only a filename, it will search in the current working directory.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement