Skip to content

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

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

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