I want to remove the folders containing 'Done' in the folder name. Here is my code
import os
mainPath = os.getcwd() + "/MyFolder/"
folderList = os.listdir(mainPath)
tempList = folderList # so that I can iterate while using remove()
print len(tempList)
print len(folderList)
for folder in tempList:
if 'Done' in folder:
folderList.remove(folder)
print len(tempList) # expected: Not to change
print len(folderList)
The output I get is:
26
26
22
22
I do not understand why it is deleting items from testList. Shouldn't it delete only from folderList?