Automate copy and paste file with erase the content of file if exists on destination directory. Like shutil.copytree but when dir is exists on destination tree , don't fail, just pass.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  | import os
import sys
import shutil
__doc__ = """ copy all files from  directory to an destination ,
It recreate directory tree of src to dst and replace or create file in this directory
If you have tree like this
src/A/dir/file
src/B/dir/sdir/file2
src/B/dir/file
and
then after script src dst 
dst/dir/file
dst/dir/sdir/file2 -> this is the B file2 wich is taken"""
def copytree(src, dst):
    
    if os.path.isdir(src):
        if not os.path.exists(dst):
            os.makedirs(dst)
        for name in os.listdir(src):
            copytree(os.path.join(src, name),
                     os.path.join(dst, name))
    else:
        shutil.copyfile(src, dst)
def main(dsrc, ddst):
    for dirname in os.listdir(dsrc):
        tocopy = os.path.join(dsrc, dirname)
        for d in os.listdir(tocopy):
            src = os.path.join(tocopy,d)
            dst = os.path.join(ddst,d)
            if os.path.isdir(src):
                copytree(src, dst)
if __name__ == '__main__':
    src = sys.argv[1]
    dst = sys.argv[2]
    main(src, dst)
    
 | 
copy all files from directory to an destination , It recreate directory tree of src to dst and replace or create file in this directory If you have tree like this src/A/dir/file src/B/dir/sdir/file2 src/B/dir/file and then after script src dst dst/dir/file dst/dir/sdir/file2 -> this is the B file2 wich is taken
Download
Copy to clipboard