3

I know two methods of copying a directory recursively from source to destination. 1) cp -r <source> <destination> 2) shutil.copytree(<source>, <destination>)

Method 1 is linux standard method. method 2 is pythonic way of copying a directory by importing shutil module within a python script.

Can anyone explain me any significant/fundamental difference between working of the two ? Which one is faster ? Which one is safer ? Since i can run cp -r command from within python script by python's os.system() function, i want to know which method should i use if i want to copy directories within a python script ?

sherelock
  • 464
  • 5
  • 9
  • What if your script using `os.system` is run on Windows? It definitely won't work, while `shutil` is cross-platform, just like Python itself. – ForceBru Nov 14 '17 at 16:22
  • 1
    `cp` is a binary from your linux distro. `shutil.copytree` is bunch of Python code. – Łukasz Rogalski Nov 14 '17 at 16:23
  • good point @Forcebru. – sherelock Nov 14 '17 at 16:44
  • Also wondering about this. the `copytree` page gives a bunch of warnings: https://docs.python.org/3/library/shutil.html ; to the point where I think it's easier to just invoke a subprocess shell and copy things that way – information_interchange Aug 05 '21 at 15:18
  • Indeed, we can see here that spawning a new subprocess might be the better approach: https://stackoverflow.com/questions/12683834/how-to-copy-directory-recursively-in-python-and-overwrite-all – information_interchange Aug 05 '21 at 15:21

0 Answers0