Eryk Sun <> 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 

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


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

    t0 = time.perf_counter()

    while True:
            wait_time = time.perf_counter() - t0
        except OSError as e:
            if e.winerror != ERROR_DIR_NOT_EMPTY:


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

Python tracker <>
Python-bugs-list mailing list

Reply via email to