Python: copy folder content recursively

malt3

I want to copy the content of a folder recursively without copying files that already exist. Also, the destination Folder already exists and contains files. I tried to use shutils.copytree(source_folder, destination_folder), but it does not do what I want.

I want it to work like this:

Before:

  • source_folder
    • sub_folder_1
      • foo
      • bar
    • sub_folder_2

  • destination_folder
    • folder_that_was_already_there
      • file2.jpeg
    • some_file.txt
    • sub_folder_1
      • foo

After:

  • destination_folder
    • folder_that_was_already_there
      • file2.jpeg
    • some_file.txt
    • sub_folder_1
      • foo
      • bar
    • sub_folder_2
malt3

I found the answer with the help of tdelaney:
source_folder is the path to the source and destination_folder the path to the destination.

import os
import shutil

def copyrecursively(source_folder, destination_folder):
for root, dirs, files in os.walk(source_folder):
    for item in files:
        src_path = os.path.join(root, item)
        dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
        if os.path.exists(dst_path):
            if os.stat(src_path).st_mtime > os.stat(dst_path).st_mtime:
                shutil.copy2(src_path, dst_path)
        else:
            shutil.copy2(src_path, dst_path)
    for item in dirs:
        src_path = os.path.join(root, item)
        dst_path = os.path.join(destination_folder, src_path.replace(source_folder, ""))
        if not os.path.exists(dst_path):
            os.mkdir(dst_path)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: copy folder content recursively

From Dev

How to recursively copy file an folder in .Net?

From Dev

Cannot delete folder - Content seems to be nested recursively

From Dev

Download folder with all content recursively, Sharpbox

From Dev

Copy recursively a directory excluding a subdirectory and ITS CONTENT

From Dev

Find all files, and copy them to a folder (Flatten recursively)

From Dev

Inno Setup: copy folder, subfolders and files recursively in Code section

From Dev

Find all files, and copy them to a folder (Flatten recursively)

From Dev

Windows Command Copy all files recursively to a Main Folder

From Dev

How to copy a folder recursively in an idempotent way using cp?

From Dev

Powershell Copy-Item recursively but don't include folder name

From Dev

Copy pictures recursively from one folder to the root of another?

From Dev

Copy and rename files recursively using part of folder name for new name

From Dev

Copy files recursively from a hierarchy to a single flat folder

From Dev

Copy content of zipfile in Python

From Dev

Powershell Script to Copy Folder and Content to remote location

From Dev

Copy all content inside of subfolders to the main folder?

From Dev

Laravel: Create a new folder and copy the content

From Dev

Copy content from one folder to another

From Dev

Windows cmd copy directories and content recursively if the files do not exist

From Dev

copy files from folder to folder python

From Dev

copy files from folder to folder python

From Dev

Copy all content from the folder into two file destination folder

From Dev

How to copy file/folder permissions and ownerships from one folder to an other (recursively)

From Dev

How do I list (ls) the content of a folder/directory recursively but to a depth of only one folder/directory?

From Dev

Calculate Each dropbox folder size recursively using python api

From Dev

Calculate Each dropbox folder size recursively using python api

From Dev

Python script recursively rename all files in folder and subfolders

From Dev

cordova 5.3.1 doesn't copy www folder ( in root folder of project) content to asset folder and app

Related Related

  1. 1

    Python: copy folder content recursively

  2. 2

    How to recursively copy file an folder in .Net?

  3. 3

    Cannot delete folder - Content seems to be nested recursively

  4. 4

    Download folder with all content recursively, Sharpbox

  5. 5

    Copy recursively a directory excluding a subdirectory and ITS CONTENT

  6. 6

    Find all files, and copy them to a folder (Flatten recursively)

  7. 7

    Inno Setup: copy folder, subfolders and files recursively in Code section

  8. 8

    Find all files, and copy them to a folder (Flatten recursively)

  9. 9

    Windows Command Copy all files recursively to a Main Folder

  10. 10

    How to copy a folder recursively in an idempotent way using cp?

  11. 11

    Powershell Copy-Item recursively but don't include folder name

  12. 12

    Copy pictures recursively from one folder to the root of another?

  13. 13

    Copy and rename files recursively using part of folder name for new name

  14. 14

    Copy files recursively from a hierarchy to a single flat folder

  15. 15

    Copy content of zipfile in Python

  16. 16

    Powershell Script to Copy Folder and Content to remote location

  17. 17

    Copy all content inside of subfolders to the main folder?

  18. 18

    Laravel: Create a new folder and copy the content

  19. 19

    Copy content from one folder to another

  20. 20

    Windows cmd copy directories and content recursively if the files do not exist

  21. 21

    copy files from folder to folder python

  22. 22

    copy files from folder to folder python

  23. 23

    Copy all content from the folder into two file destination folder

  24. 24

    How to copy file/folder permissions and ownerships from one folder to an other (recursively)

  25. 25

    How do I list (ls) the content of a folder/directory recursively but to a depth of only one folder/directory?

  26. 26

    Calculate Each dropbox folder size recursively using python api

  27. 27

    Calculate Each dropbox folder size recursively using python api

  28. 28

    Python script recursively rename all files in folder and subfolders

  29. 29

    cordova 5.3.1 doesn't copy www folder ( in root folder of project) content to asset folder and app

HotTag

Archive