[Tutor] Request for help with os.walk() combining os.path.ismount() in Linux

Srihari Vijayaraghavan linux.bug.reporting at gmail.com
Fri Aug 21 04:10:58 CEST 2015

On 21 August 2015 at 02:57, Alan Gauld <alan.gauld at btinternet.com> wrote:
> On 20/08/15 09:51, Srihari Vijayaraghavan wrote:
>> In general I agree, but this is what the os.walk() document states:
>> "... When topdown is True, the caller can modify the dirnames list
>> in-place (perhaps using del or slice assignment)..."
> That's true so far as the os.walk call goes.
> That is, altering dirs does not have any nagative
> impact on the subsequent iterations of os.walk.
> So even if you delete items from dirs os.walk will
> continue to iterate over those deleted directories
> in subsequent calls to os.walk()

Thanks Alan. That's now practically learnt :-).

> But altering dirs within the current iteration does affect
> dirs within that iteration, just like any other collection.
> So your for loop inside the os.walk loop is affected by the
> deletions even if the outer os.walk loop is not.

Yes, it was a bad idea to update dirs list while iterating over it. I
was confused/mislead by the documentation, perhaps by misinterpreting
it. Never mind, now it's quite clear that it's a no-no.

Thanks your quick & perfect analysis.

Srihari Vijayaraghavan

More information about the Tutor mailing list