Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

shutil.rmtree() cannot remove directory trees deeper than MAX_PATH on Windows #159681

Open

Description

@serhiy-storchaka

On Windows, shutil.rmtree() uses the path-based implementation, which builds the full path of every file and directory. Unless long paths are enabled in the registry (LongPathsEnabled, off by default), paths longer than MAX_PATH (260 characters) cannot be accessed. os.walk() ignores the resulting FileNotFoundError, so the deep part of the tree is silently skipped, and rmtree() then fails with "The directory is not empty".

import os, shutil
path = '\\\\?\\' + os.path.abspath('deep')
os.mkdir(path)
for i in range(200):
    path += '\\d'
    os.mkdir(path)
shutil.rmtree('deep')
OSError: [WinError 145] The directory is not empty: 'deep\\d\\d\\d\\...'

Using extended-length paths (\\?\C:\...) will help, but as a side effect they can leak to the user via errors.

Linked PRs

Activity

  1. added
    type-bug
    stdlib
    3.14
    3.15
    3.16
    on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

3.143.153.16OS-windowsstdlibtype-bug

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions