Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to skip existing files in sub folders and copy only new files

I am copy folder and all sub folders inside the main folder using shutil copytree

import shutil
import sys
import os
import re

SOURCE_FOLDER = sys.argv[1]
DESTINATION_FOLDER = sys.argv[2]


def copyDirectory(SOURCE_FOLDER, DESTINATION_FOLDER):
    try:
        print SOURCE_FOLDER
        print DESTINATION_FOLDER
        shutil.copytree(SOURCE_FOLDER, DESTINATION_FOLDER)
    # Directories are the same
    #except:
    #   print "Not copied"
    except shutil.Error as e:
        print('Directory not copied. Error: %s' % e)
    # Any error saying that the directory doesn't exist
    except OSError as e:
        print('Directory not copied. Error: %s' % e)

copyDirectory(SOURCE_FOLDER,DESTINATION_FOLDER)

The problem is if the directory exists it throws error

Directory not copied. Error: [Errno 17] File exists: 'destination'

What i want is if directory already exists it want to check all the sub directories and if sub directory also exists it should check all the files in it and it should skip the existing files and copy the new files in that sub directory,If sub direscotry not exists then it should copy that sub directory

Note: Sub directories might be nested(Sub directory of sub directory).

But the above script is not working what should i add to that script?


1 Answers

From python3.8, you can use shutil.copytree with an option to ignore existing dirs error:

shutil.copytree(SOURCE_FOLDER, DESTINATION_FOLDER, dirs_exist_ok=True)

More info can be found here: https://docs.python.org/3/library/shutil.html#shutil.copytree

like image 168
Eugene Dudnyk Avatar answered Sep 19 '25 05:09

Eugene Dudnyk