3

I am sure I am missing this, as it has to be easy, but I have looked in Google, The Docs, and SO and I can not find a simple way to copy a bunch of directories into another directory that already exists from Python 3?

All of the answers I find recommend using shutil.copytree but as I said, I need to copy a bunch of folders, and all their contents, into an existing folder (and keep any folders or that already existed in the destination)

Is there a way to do this on windows?

Community
  • 1
  • 1
Startec
  • 12,496
  • 23
  • 93
  • 160
  • here is the actual implementation of copy tree ...https://docs.python.org/2/library/shutil.html#copytree-example it should be easy enough to change it to allow a directory that already exists and just skip files that already exist – Joran Beasley Sep 29 '15 at 00:31
  • what does it mean *"not overwrite"* -- do you want skip existing files or do you want to skip existing directory trees (except the root)? There are many options: if you don't limit the choice; you might get something like `rsync`. – jfs Sep 29 '15 at 03:02
  • @J.F.Sebastian edited my question. I just meant to leave directories and files that were already there in place. – Startec Sep 29 '15 at 03:15
  • Does *"keep"* mean "avoid updating files with new content" I.e., the copy may only add files and directories? Do you want to preserve hard links? Do you want to follow symlinks? Do you want to preserve special file permissions or other auxiliary file data? – jfs Sep 29 '15 at 03:35
  • Exactly what I want is that every directory inside of directory A, goes into directory B. I would like every directory in directory B to remain inside of directory B (untouched), unless it exists in directory A, in which case it should be overwritten. Basically, I want to be able to do `os.copytree` but for a directory that already exists. – Startec Sep 29 '15 at 03:43
  • 2
    it seems that all you need is to replace `os.makedirs(dst)` with `os.makedirs(dst, exist_ok=True)` in the [`copytree()`](https://github.com/python/cpython/blob/2ed5b9f7264084f4119653505f254441e1d626d4/Lib/shutil.py#L309) – jfs Sep 29 '15 at 05:01
  • Possible duplicate of [How do I copy an entire directory of files into an existing directory using Python?](http://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth) – R__raki__ Nov 01 '16 at 10:13

1 Answers1

0

I would look into using the os module built into python. Specifically os.walk which returns all the files and subdirectories within the directory you point it to. I will not just tell you exactly how to do it, however here is some sample code that i use to backup my files. I use the os and zipfile modules.

import zipfile
import time
import os

def main():
    date = time.strftime("%m.%d.%Y")

    zf = zipfile.ZipFile('/media/morpheous/PORTEUS/' + date, 'w')

    for root, j, files in os.walk('/home/morpheous/Documents'):
        for i in files:
            zf.write(os.path.join(root, i))
zf.close()

Hope this helps. Also it should be pointed out that i do this in linux, however with a few changes to the file paths it should work the same

Ellis
  • 105
  • 2
  • 10