[issue33240] shutil.rmtree fails when the inner floder is opened in Explorer on Windows

Eryk Sun report at bugs.python.org
Sun Apr 8 00:35:51 EDT 2018


Eryk Sun <eryksun at gmail.com> added the comment:

This is not an uncommon problem. If there's one or more existing references to a file or empty directory that were opened with shared delete access, then a delete operation will succeed, but the file or directory will not be unlinked from the parent directory. The filesystem only unlinks a file or directory if its "delete disposition" is set when the last reference is closed. 

Removing the parent directory thus requires a loop that retries the delete until it succeeds, i.e. until existing references are closed and the directory finally becomes empty and thus deletable. If the problem is caused by an anti-malware program, it should typically be resolved within a short time. Exactly how long to wait in a retry loop before failing the operation should be configurable.

Maybe you can conduct a simple experiment to measure the wait time required in your case. Run the following with "bar" opened in Explorer. Substitute the real path of "foo" in PARENT_PATH.

    import os
    import time

    ERROR_DIR_NOT_EMPTY = 145

    PARENT_PATH = 'foo'
    CHILD_PATH = os.path.join(PARENT_PATH, 'bar')

    os.rmdir(CHILD_PATH)
    t0 = time.perf_counter()

    while True:
        try:
            os.rmdir(PARENT_PATH)
            wait_time = time.perf_counter() - t0
            break
        except OSError as e:
            if e.winerror != ERROR_DIR_NOT_EMPTY:
               raise

    print(wait_time)

----------
components: +IO
nosy: +eryksun
stage:  -> test needed
versions: +Python 3.7, Python 3.8

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue33240>
_______________________________________


More information about the Python-bugs-list mailing list