This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: list enumeration corrupt when remove()
Type: behavior Stage:
Components: Build Versions: Python 2.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: benjamin.peterson, jacek, terry.reedy
Priority: normal Keywords:

Created on 2008-08-16 19:29 by jacek, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (3)
msg71230 - (view) Author: Jacek (jacek) Date: 2008-08-16 19:29
Hi

I wrote my first program in python, and I found bug in it.
I explain it in example (I do it under Windows XP):

1. At the begining create some directories:
>mkdir .\test\
>mkdir .\test\.svn
>mkdir .\test\cvs
>mkdir .\test\pdk

2. Next create file ".\bug.py" with content:
import re
import os

print 'example1:'
lpatternDirSkip = re.compile(r'(^cvs$)|(^[.].*)', re.IGNORECASE)
for lroot, ldirs, lfiles in os.walk(r'.\\test\\'):
   ldirIndex = 0
   for ldirName in ldirs:
      if lpatternDirSkip.search(ldirName):
         ldirs.remove(ldirName)
   print ldirs

print 'example2:'
lpatternDirSkip = re.compile(r'(^cvs$)|(^[.].*)', re.IGNORECASE)
for lroot, ldirs, lfiles in os.walk('.\\test\\'):
   ldirIndex = 0
   while ldirIndex < len(ldirs):
      if lpatternDirSkip.search(ldirs[ldirIndex]):
         ldirs.remove(ldirs[ldirIndex])
         ldirIndex -= 1
      ldirIndex += 1
   print ldirs

3. Next run cmd.exe (in the same directory) and type "bug.py". Result is:
example1:
['cvs', 'pdk']
[]
[]
example2:
['pdk']
[]

5. Comment:
In this example I want to remove from list of directories (ldirs) every
hiden directories (like ".svn") and directory "CVS". 
Example1 is the comfortable way, but it products wrong result (the "cvs"
directory is not remove). This is only happen when I remove some
directories from the list. I don't care that there was deleted one
element from the list. It should be special case, and enumeration on the
rest elements should be correct.
Example2 works correcty (it's work around of this problem).

Jacek Jaworski
msg71232 - (view) Author: Benjamin Peterson (benjamin.peterson) * (Python committer) Date: 2008-08-16 19:31
It's a known fact that mutating a list during iteration will cause
problems. You should mutate a copy of the list.
msg71236 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2008-08-16 20:45
Since you are just beginning Python, I suggest you report questionable
program behavior on newsgroups comp.lang.python,
gmane.comp.python.general at news.gmane.org, or the python.org mailing
list python-list (all three are equivalent).
History
Date User Action Args
2022-04-11 14:56:37adminsetgithub: 47818
2008-08-16 20:45:41terry.reedysetnosy: + terry.reedy
messages: + msg71236
2008-08-16 19:31:19benjamin.petersonsetstatus: open -> closed
resolution: not a bug
messages: + msg71232
nosy: + benjamin.peterson
2008-08-16 19:29:17jacekcreate