Skip to content

shutil.copytree() fails to override existing symbolic links #105919

Open

Description

@SPnRD

Bug report

Per documentation shutil.copytree() with dirs_exist_ok=True should override all the files. However, shutil.copytree() fails to override existing symbolic links to files in the directory structure.

Resulting error:

*Exception has occurred: Error
[('src/dir/link', 'dst/dir/link', "[Errno 17] File exists: '../top.txt' -> 'dst/dir/link'")]
  File "/home/bdudykevych/projects/89220/BatchScripts/bug.py", line 17, in <module>
    shutil.copytree('src', 'dst', symlinks=True, ignore=None, dirs_exist_ok=True, ignore_dangling_symlinks=True)
shutil.Error: [('src/dir/link', 'dst/dir/link', "[Errno 17] File exists: '../top.txt' -> 'dst/dir/link'")]*

Code to reproduce the issue:

import os
import shutil
#cleanup
if os.path.isdir('src'):
	shutil.rmtree('src')
if os.path.isdir('dst'):
	shutil.rmtree('dst')
os.mkdir('src')
os.mkdir('src/dir')
os.mkdir('dst')
with open('src/top.txt', "w") as constrs:
	constrs.write("dummy")
if not os.path.exists('src/dir/link'):
	if os.path.isfile(os.path.join(os.getcwd(),'src/top.txt')):
		os.symlink('../top.txt', 'src/dir/link')
#Inital copy to empty location. Here it works as there is no link in the destination
shutil.copytree('src', 'dst', symlinks=True, ignore=None, dirs_exist_ok=True, ignore_dangling_symlinks=True)
#2nd launch, fails to override the symlink and results in an error
shutil.copytree('src', 'dst', symlinks=True, ignore=None, dirs_exist_ok=True, ignore_dangling_symlinks=True)

Environment

  • Python versions tested on: 3.11.2,
  • Operating system : RHEL 7.9 (Maipo), 3.10.0-1160.76.1.el7.x86_64

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Jun 19, 2023
  2. added
    stdlibStandard Library Python modules in the Lib/ directory
    on Jun 19, 2023
  3. terryjreedy commented on Jun 20, 2023

    @terryjreedy
    Member

    @SPnRD Use triple backticks ``` above and below code and traceback blocks to format properly, including indents.
    I redid both above. Click ... on title bar and select `edit` to see raw text.

  4. gaitovrat commented on Jul 31, 2024

    @gaitovrat

    @terryjreedy Any progress here?

  5. athre4t commented on Jul 31, 2024

    @athre4t

    @terryjreedy Any progress here?

  6. terryjreedy commented on Aug 1, 2024

    @terryjreedy
    Member

    My participation was strictly as triager helping OP format code.
    @giampaolo still active?
    One of you should use git blame to see who has worked on shutil more recently than giampaolo. (I likely have never done so.) When found, ping here and request review on the PR.

  7. gaitovrat commented on Aug 1, 2024

    @gaitovrat

    @barneygale could you review the PR?

  8. Rid1FZ commented on Sep 11, 2024

    @Rid1FZ

    I am also having this issue. This is counter intuitive as in documentation it is said that dirs_exist_ok=True will overwrite anything if exists. But I am getting error where it is not being able to overwrite symlinks to files(I didn't checked symlinks to directories). I am getting similar error saying that file already exists...

    ---------------------------------------------------------------------------
    Error                                     Traceback (most recent call last)
    File ~/repos/test/python-test/test.py:3
          1 import shutil
    ----> 3 shutil.copytree(
          4     "./profile.d",
          5     "./profile.d.bak",
          6     symlinks=True,
          7     dirs_exist_ok=True,
          8     copy_function=shutil.copy2,
          9     ignore_dangling_symlinks=True,
         10 )
    
    File ~/.mamba/lib/python3.12/shutil.py:600, in copytree(src, dst, symlinks, ignore, copy_function, ignore_dangling_symlinks, dirs_exist_ok)
        598 with os.scandir(src) as itr:
        599     entries = list(itr)
    --> 600 return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
        601                  ignore=ignore, copy_function=copy_function,
        602                  ignore_dangling_symlinks=ignore_dangling_symlinks,
        603                  dirs_exist_ok=dirs_exist_ok)
    
    File ~/.mamba/lib/python3.12/shutil.py:554, in _copytree(entries, src, dst, symlinks, ignore, copy_function, ignore_dangling_symlinks, dirs_exist_ok)
        552         errors.append((src, dst, str(why)))
        553 if errors:
    --> 554     raise Error(errors)
        555 return dst
    
    Error: [('./profile.d/functions.zsh', './profile.d.bak/functions.zsh', "[Errno 17] File exists: 'plugins.sh' -> './profile.d.bak/functions.zsh'"), ('./profile.d/aliases.zsh', './profile.d.bak/aliases.zsh', "[Errno 17] File exists: 'aliases.sh' -> './profile.d.bak/aliases.zsh'"), ('./profile.d/env_vars.zsh', './profile.d.bak/env_vars.zsh', "[Errno 17] File exists: 'env_vars.sh' -> './profile.d.bak/env_vars.zsh'")]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions