This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: add exist_ok to shutil.copytree
Type: enhancement Stage: commit review
Components: Library (Lib) Versions: Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: giampaolo.rodola Nosy List: elias, eric.araujo, giampaolo.rodola, hynek, jab, justin.myers, ncoghlan, r.david.murray, tarek, thehesiod
Priority: normal Keywords: patch

Created on 2014-03-04 04:55 by thehesiod, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
shutil-copytree-exist_ok.patch elias, 2014-03-04 05:21 Issue 20849: add exist_ok to shutil.copytree
Pull Requests
URL Status Linked Edit
PR 2977 closed Ofekmeister, 2017-08-01 21:36
PR 8792 merged jab, 2018-08-16 18:58
Messages (14)
msg212691 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-04 04:55
it would be REALLY nice (and REALLY easy) to add a parameter: exist_ok and pass this to os.makedirs with the same parameter name so you can use copytree to append a src dir to an existing dst dir.
msg212692 - (view) Author: Elias Zamaria (elias) * Date: 2014-03-04 05:21
Here is a patch that adds the option.

I am not very familiar with the internals of shutil and os so I would recommend that someone else review it.

I haven't added any tests. I can try to if anyone wants but I am not sure how long it will take me or if I will find the time any time soon.
msg212756 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-05 01:36
awesome, thanks so much!!
msg212901 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2014-03-07 21:41
Contrary to makedirs, there could be two interpretations for exist_ok in copytree: a) if a directory or file already exists in the destination, ignore it and go ahead  b) only do that for directories.

The proposed patch does b), but the cp tool does a).  It’s not clear to me which is best.  Can you start a discussion on the python-ideas mailing list?
msg212945 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-08 23:18
how about instead we rename the new parameter to dirs_exists_ok or something like that since the method already allows for existing files.
msg212948 - (view) Author: Elias Zamaria (elias) * Date: 2014-03-09 00:03
I am not sure. I am not on the python-ideas mailing list, and I am not sure
what adding and maintaining the discussion would entail, or if I would have
the time to do it or want to deal with the clutter in my inbox. I just
committed this patch because it seemed like it would be quick and easy.

I can start the discussion if anyone specifically wants me to, but I don't
want to let anyone down.

On Fri, Mar 7, 2014 at 1:41 PM, Éric Araujo <report@bugs.python.org> wrote:

>
> Éric Araujo added the comment:
>
> Contrary to makedirs, there could be two interpretations for exist_ok in
> copytree: a) if a directory or file already exists in the destination,
> ignore it and go ahead  b) only do that for directories.
>
> The proposed patch does b), but the cp tool does a).  It's not clear to me
> which is best.  Can you start a discussion on the python-ideas mailing list?
>
> ----------
> nosy: +eric.araujo
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue20849>
> _______________________________________
>
msg212949 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-09 01:10
I personally dont think this is worth investing the time for a discussion.
If the maintainers dont want to accept this or a minor variation without a
discussion ill just keep my local monkeypatch :)  thanks again for the
quick patch Elias!
On Mar 8, 2014 4:03 PM, "Elias Zamaria" <report@bugs.python.org> wrote:

>
> Elias Zamaria added the comment:
>
> I am not sure. I am not on the python-ideas mailing list, and I am not sure
> what adding and maintaining the discussion would entail, or if I would have
> the time to do it or want to deal with the clutter in my inbox. I just
> committed this patch because it seemed like it would be quick and easy.
>
> I can start the discussion if anyone specifically wants me to, but I don't
> want to let anyone down.
>
> On Fri, Mar 7, 2014 at 1:41 PM, Éric Araujo <report@bugs.python.org>
> wrote:
>
> >
> > Éric Araujo added the comment:
> >
> > Contrary to makedirs, there could be two interpretations for exist_ok in
> > copytree: a) if a directory or file already exists in the destination,
> > ignore it and go ahead  b) only do that for directories.
> >
> > The proposed patch does b), but the cp tool does a).  It's not clear to
> me
> > which is best.  Can you start a discussion on the python-ideas mailing
> list?
> >
> > ----------
> > nosy: +eric.araujo
> >
> > _______________________________________
> > Python tracker <report@bugs.python.org>
> > <http://bugs.python.org/issue20849>
> > _______________________________________
> >
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue20849>
> _______________________________________
>
msg213611 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2014-03-15 01:44
Adding some core devs to get their opinion on this proposal.
msg213620 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2014-03-15 02:15
I don't know what "the method already allows for existing files" means.  Since the target directory can't exist, there can be no existing files.

In unix, this kind of capability is provided by a combination of shell globbing and 'cp -r', and by default it does replace existing files.  So it would be reasonable for exists_ok to mean exactly that: replace anything that currently exists, if it does.

I think that would be a reasonable API, but the implementation isn't as simple as just passing through the exists_ok flag to makedirs.

I do not think that *just* making it OK for the destination directory to exist would be a good API.
msg213647 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-15 09:01
Ya. The original request I think is ok because by allowing that flag it
will replace files and dirs.
On Mar 14, 2014 7:15 PM, "R. David Murray" <report@bugs.python.org> wrote:

>
> R. David Murray added the comment:
>
> I don't know what "the method already allows for existing files" means.
>  Since the target directory can't exist, there can be no existing files.
>
> In unix, this kind of capability is provided by a combination of shell
> globbing and 'cp -r', and by default it does replace existing files.  So it
> would be reasonable for exists_ok to mean exactly that: replace anything
> that currently exists, if it does.
>
> I think that would be a reasonable API, but the implementation isn't as
> simple as just passing through the exists_ok flag to makedirs.
>
> I do not think that *just* making it OK for the destination directory to
> exist would be a good API.
>
> ----------
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue20849>
> _______________________________________
>
msg214055 - (view) Author: Alexander Mohr (thehesiod) * Date: 2014-03-19 05:39
btw, I believe the solution is as simple as stated as that's what I'm doing locally and its behaving exactly as intended.
msg323626 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2018-08-16 22:22
I think this feature request is reasonable for 2 reasons:

1) As it stands if dst directory exists copytree() cannot be used. The only possible workaround is using rmtree(dst) first, but that doesn't seem to make much sense. The change may look extremely easy but the fact that copytree() cannot be used in a specific circumstance justifies the cost of introducing a new argument, basically because there is no other way around it.

2) "cp -r" does this by default, which is probably indicative (I don't think the default should be True though).

I think the new argument should be called "dirs_exists_ok" instead of "exists_ok" though, so that it's clear that the behavior does not affect files.

[ R. David Murray ]
> I do not think that *just* making it OK for the destination directory to exist would be a good API.

I don't think that refusing (or allowing) to copy existing files is a common enough use case to justify the addition of a new parameter (note: also "cp" does not provide this use case). If such a  behavior is desired the user can simply provide its own custom "copy_function".
msg323665 - (view) Author: Joshua Bronson (jab) * Date: 2018-08-17 17:45
I submitted a new PR in https://github.com/python/cpython/pull/8792 that addresses the outstanding concerns in @ofekmeister's original PR. It includes passing tests and passes all the GitHub PR status checks. Does it look ok to merge? Thanks.
msg332670 - (view) Author: Giampaolo Rodola' (giampaolo.rodola) * (Python committer) Date: 2018-12-28 18:03
New changeset 9e00d9e88fbf943987e4771c753f5ca8f794103e by Giampaolo Rodola (jab) in branch 'master':
bpo-20849: add dirs_exist_ok arg to shutil.copytree (patch by Josh Bronson)
https://github.com/python/cpython/commit/9e00d9e88fbf943987e4771c753f5ca8f794103e
History
Date User Action Args
2022-04-11 14:57:59adminsetgithub: 65048
2018-12-28 18:05:08giampaolo.rodolasetstatus: open -> closed
assignee: giampaolo.rodola
resolution: fixed
stage: patch review -> commit review
2018-12-28 18:03:44giampaolo.rodolasetmessages: + msg332670
2018-08-17 17:45:14jabsetmessages: + msg323665
2018-08-17 17:41:31jabsetnosy: + jab
2018-08-16 22:22:20giampaolo.rodolasetmessages: + msg323626
2018-08-16 18:58:06jabsetpull_requests: + pull_request8266
2018-06-12 09:43:10giampaolo.rodolasetnosy: + giampaolo.rodola
2017-12-09 03:47:52martin.panterlinkissue32247 superseder
2017-08-01 21:36:24Ofekmeistersetpull_requests: + pull_request3020
2015-03-12 13:07:10r.david.murraylinkissue23638 superseder
2014-04-23 19:36:59justin.myerssetnosy: + justin.myers
2014-03-19 05:39:11thehesiodsetmessages: + msg214055
2014-03-15 09:01:29thehesiodsetmessages: + msg213647
2014-03-15 02:15:54r.david.murraysetmessages: + msg213620
2014-03-15 01:44:14eric.araujosetnosy: + ncoghlan, tarek, r.david.murray, hynek
messages: + msg213611
2014-03-09 01:10:42thehesiodsetmessages: + msg212949
2014-03-09 00:03:23eliassetmessages: + msg212948
2014-03-08 23:18:12thehesiodsetmessages: + msg212945
2014-03-07 21:41:50eric.araujosetnosy: + eric.araujo
messages: + msg212901
2014-03-05 01:36:23thehesiodsetmessages: + msg212756
2014-03-04 14:01:27berker.peksagsetstage: patch review
versions: - Python 3.3, Python 3.4
2014-03-04 05:21:28eliassetfiles: + shutil-copytree-exist_ok.patch

nosy: + elias
messages: + msg212692

keywords: + patch
2014-03-04 04:55:22thehesiodcreate