Search code examples
pythoncopying

Python - Copying only new files into another directory


I'm trying to copy files from one directory into another ONLY if those files don't already exist.

So if I have directory A with files "1.txt, 2.txt, 3.txt" and directory B with files "1.txt, 2.txt", I want to only copy "3.txt" into directory B without rewriting the other files.

I'm a Python beginner and I've spent hours on this and have done plenty of research but I just can't seem to find the answer! Help!

Thanks


Solution

  • I'll help you out, and lead you to the answer (but I won't just outright give it to you!).

    So, first things first, most basic file editing functions are in the os module, so let's import that at the top of our script:

    import os
    

    Now let's see how we check if a file exists. [A little research shows the os.path module has a function called exists which checks if a file exists!] So now we're set, but we have to figure out how to get all the files in directory A. It looks like the os module works for this too, with the listdir function. If we have a directory called "directoryone", we could get all the files / directories in it (and then put them in a list) with this:

    [file for file in os.listdir("directoryone")]
    

    But we only want to get files, so we have to add an if statement to narrow down our list:

    [file for file in os.listdir("directoryone") if os.path.isfile(os.path.join("directoryone", f))]
    

    So now we have a statement which gets all the files in a directory, and we have a way to check if a file exists. The last thing we need to do is figure out how to copy files. We have to import the shutil model for this:

    import shutil
    

    And then we can use the shutil.copy function as so:

    shutil.copy(srcfile, dstdir)
    

    So we'd end up with this code:

    import os, shutil
    
    directorya = "exampledir"
    directoryb = "exampledir2"
    files = [file for file in os.listdir(directorya) if os.path.isfile(os.path.join(directorya, file))]
    for file in files:
        if not os.path.exists(os.path.join(directoryb, file)):
            shutil.copy(os.path.join(directorya, file), directoryb)