1

I'm writing a script to cleanup old backup folders and every second folder seems to be deleted in a sorted directory listing. I've figured out how to work around it (by sorting again prior to delete), but don't understand why the workaround is necessary.

This is the first method, which correctly sorts the array (confirmed via print), but which then deletes every second folder:

# Build an array of the relevant backup directories, excluding files.
BDL = [f for f in os.listdir(DST) if os.path.isdir(os.path.join(DST, f)) and f.startswith(PFX)]
BDL.sort()
print BDL

# If the number of backup directories is more than the minimum,
# delete some & decrement the array
logger.info('Deleting old backup directories.')
for f in BDL:
    if len(BDL) >= MIN:
        logger.info('Deleting %s', f)
        shutil.rmtree(DST+f)
        BDL.remove(f)

This is the second method which uses 'sorted' and works correctly:

# Build an array of the relevant backup directories, excluding files.
BDL = [f for f in os.listdir(DST) if os.path.isdir(os.path.join(DST, f)) and f.startswith(PFX)]
BDL.sort()
print BDL

# If the number of backup directories is more than the minimum,
# delete some & decrement the array
logger.info('Deleting old backup directories.')
for f in sorted(BDL):
    if len(BDL) >= MIN:
        logger.info('Deleting %s', f)
        shutil.rmtree(DST+f)
        BDL.remove(f)

Why is it necessary to sort the array again? Does the act of removing something from the array somehow change the order of the remaining elements?

user189271
  • 178
  • 1
  • 7
  • 1
    You shouldn't remove elements from a list while traversing it. Note that in your second example f loops through a sorted **copy** of BDL, therefore it's safe to remove it from BDL itself. Also see this: http://stackoverflow.com/q/1207406/2327880 – d0c Sep 03 '14 at 22:03
  • Thanks for that. The link explains it pretty well. – user189271 Sep 10 '14 at 23:53

0 Answers0