I have the folder /a/b/c/d/
and I want to copy d/
into destination /dst/
.
However, shutil.copytree("/a/b/c/d", "/dst")
produces /dst/a/b/c/d
.
I only want /dst
, or even /dst/d
would suffice, but I don't want all the intermediate folders.
[edit]
As others have noted, copytree does what I want - I had unintentionally added the full path of the source to my destination!
Given this file structure (on my directory /tmp
):
a
└── b
└── c
└── d
├── d_file1
├── d_file2
├── d_file3
└── e
├── e_file1
├── e_file2
└── e_file3
If you do shutil.copytree("/tmp/a", "/tmp/dst")
you will get:
dst
└── b
└── c
└── d
├── d_file1
├── d_file2
├── d_file3
└── e
├── e_file1
├── e_file2
└── e_file3
But if you do shutil.copytree('/tmp/a/b/c/d', '/tmp/dst/d')
you get:
dst
└── d
├── d_file1
├── d_file2
├── d_file3
└── e
├── e_file1
├── e_file2
└── e_file3
And shutil.copytree('/tmp/a/b/c/d', '/tmp/dst')
:
dst
├── d_file1
├── d_file2
├── d_file3
└── e
├── e_file1
├── e_file2
└── e_file3
shutil.copytree
also takes relative paths. You can do:
import os
os.chdir('/tmp/a/b/c/d')
shutil.copytree('.', '/tmp/dst')
Or, since Python 3.6, you can use pathlib arguments to do:
from pathlib import Path
p=Path('/tmp/a/b/c/d')
shutil.copytree(p, '/tmp/dst')
Either case, you get the same result as shutil.copytree('/tmp/a/b/c/d', '/tmp/dst')