0

copy_tree copies the files and folders under src to dst, it's same as cp -r a/b/* x/y/ in shell

$ tree
.
|__a
|  |__b
|     |__ab.txt
|__x
   |__y

>>> from distutils.dir_util import copy_tree
>>> copy_tree('a/b','x/y')
['x/y/aa.txt']
>>> 

How to make it copy the folder b as is? like doing cp -r a/b x/y/

I need to copy b into x/y, so it becomes x/y/b

wim
  • 338,267
  • 99
  • 616
  • 750
rodee
  • 3,233
  • 5
  • 34
  • 70
  • 1
    Possible duplicate of [How do I copy an entire directory of files into an existing directory using Python?](https://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth) – John Szakmeister Mar 04 '19 at 22:50

2 Answers2

1

Try to use shutil library.

import shutil
shutil.copytree('a/b','x/y/b')
# Returns 'x/y/b'

See the doc for more info.

gdlmx
  • 6,479
  • 1
  • 21
  • 39
  • This question seems to be a duplication of ["How do I copy an entire directory of files into an existing directory using Python?"](https://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth) – gdlmx Mar 04 '19 at 22:17
1

You can always use os.system to use shell commands. It comes handy when you don't know pythonic way but you know bash way. I use it this way.

import os
os.system("<command that you want to run in shell>")

In this case, you need

import os
os.system("cp -r a/b x/y/")
CodeTalker
  • 1,683
  • 2
  • 21
  • 31