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?