New submission from Remy Roy:

On Windows, os.scandir will keep a handle on the directory being scanned until 
the iterator is exhausted. This behavior can cause various problems if try to 
use some filesystem calls like os.chmod or os.remove on the directory while the 
handle is still being kept.

There are some use cases where the iterator is not going to be exhausted like 
looking for a specific entry in a directory and breaking from the loop 
prematurely.

This behavior should at least be documented.  Alternatively, it might be 
interesting to provide a way prematurely end the scan without having to exhaust 
it and close the handle.

As a workaround, you can force the exhaustion after you are done with the 
iterator with something like:

for entry in iterator:
    pass

This is going to affect os.walk as well since it uses os.scandir .

The original github issue can be found on 
https://github.com/benhoyt/scandir/issues/58 .

----------
components: Windows
messages: 258212
nosy: paul.moore, remyroy, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: On Windows, os.scandir will keep a handle on the directory until the 
iterator is exhausted
type: behavior
versions: Python 3.5

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue26111>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to