Reputation: 3829
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!
Upvotes: 0
Views: 544
Reputation: 103884
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')
Upvotes: 1
Reputation: 1894
that is what shutil.copytree
does !
shutil.copytree recursively copies an entire directory tree rooted at src to a directory named dst
so the statement in the question :
shutil.copytree("/a/b/c/d", "/dst")
produces /dst/a/b/c/d
.
is just wrong.this will copy the contents of d
(and subdirectories) in to /dst
Upvotes: 1