0

Folder1:

tree
.
├── abc.json
├── pqr.bson
├── subdir
│   ├── abc.json
│   ├── pqr.bson
│   └── xyz.json
└── xyz.json

1 directory, 6 files

import os
import shutil
sourcefolder="./Folder1"
destinationfolder="./Folder2"    
if os.path.exists(destinationfolder):
    shutil.rmtree(destinationfolder)
shutil.copytree(sourcefolder,destinationfolder)

Folder2:

tree
.
├── abc.json
├── pqr.bson
├── subdir
│   ├── abc.json
│   ├── pqr.bson
│   └── xyz.json
└── xyz.json

1 directory, 6 files

I would like copy along with source folder as well something like below under Folder2:

Folder2:

    tree
├── Folder1
│   ├── abc.json
│   ├── pqr.bson
│   ├── subdir
│   │   ├── abc.json
│   │   ├── pqr.bson
│   │   └── xyz.json
│   └── xyz.json

Is there a way we can do this with in copytree ?

Achar007
  • 67
  • 6

1 Answers1

1

I had the same question and I used Pathlib.

from path import Pathlib
import shutil

shutil.copytree(sourcefolder, destinationfolder / sourcefolder.name)
Awesome_Byte
  • 11
  • 1
  • 3