Copy entire directory with python

Asked

Viewed 1,245 times

0

Hello, I would like to copy a whole directory using python, because I have a site base ready and every time I will create a new project I have to keep moving the files to a new folder. I would do that without moving those files and rather give a Ctrl-c on them and Ctrl-v in the destination folder.

It would be possible to do this with python?

  • What have you tried so far ? You can show a bit of your code ?

  • No, actually I only saw a few examples here inside the stack, but I didn’t actually create anything.

  • Unfortunately the examples here were only to move files from within directories to another, but never an entire folder.

  • Read about the module shutil.

  • Vlw same guy. I will try to find the solution here. Thank you!

1 answer

1


You can use the library shutil

Note that it has a warning about the inability to copy metadata, you may encounter other problems according to your OS.

The best solution would be to write the Bash copy script on linux or shell (cmd) in windows and call the routine from within Python using subprocess. It can be as simple as:

import subprocess
in_dir = #"pasta a copiar"
out_dir = #"local de destino"
rotina = ["cp",in_dir,out_dir] # exemplo usando Bash
processo = subprocess.run(rotina)
  • Cool guy, vlw even. I will definitely try to reproduce this idea. vlw

  • You’re welcome! If the answer worked, mark as accepted.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.