Copy directory contents to directory using python

I have a directory / a / b / c that has files and subdirectories. I need to copy the directory / a / b / c / * to the directory / x / y / z. What python methods can i use?

I tried shutil.copytree("a/b/c", "/x/y/z") , but python tries to create / x / y / z and causes an error "Directory exists" .

+38
python shutil copytree
Feb 22 '13 at 22:19
source share
3 answers

I found this code working.

 from distutils.dir_util import copy_tree # copy subdirectory example fromDirectory = "/a/b/c" toDirectory = "/x/y/z" copy_tree(fromDirectory, toDirectory) 
+66
Feb 22 '13 at 22:36
source share

You can also use glob2 to recursively collect all paths (using subdirectories ** subfolders), and then use the shutil.copy file, keeping the paths

glob2 link: https://code.activestate.com/pypm/glob2/

+1
Aug 29 '14 at 5:55
source share

Python libraries are deprecated by this feature. I made one that works correctly:

 import os import shutil def copydirectorykut(src, dst): os.chdir(dst) list=os.listdir(src) nom= src+'.txt' fitx= open(nom, 'w') for item in list: fitx.write("%s\n" % item) fitx.close() f = open(nom,'r') for line in f.readlines(): if "." in line: shutil.copy(src+'/'+line[:-1],dst+'/'+line[:-1]) else: if not os.path.exists(dst+'/'+line[:-1]): os.makedirs(dst+'/'+line[:-1]) copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1]) copydirectorykut(src+'/'+line[:-1],dst+'/'+line[:-1]) f.close() os.remove(nom) os.chdir('..') 
-four
Aug 01 '16 at 12:15
source share



All Articles