You are changing the value of path
so the rename
probably can't find a file in that particular path after the first iteration. I'm guessing maybe you mean something like
for index, file in enumerate(os.listdir(dir), 1):
while index < 21:
if file.startswith("Sub") and file.lower().endswith((".tif", ".tiff")):
path = dir + str(index)
os.makedirs(path, exists_ok=True) # create directory if it's missing
os.rename(os.path.join(dir, file), os.path.join(path , 'Oligo'))
There is obviously a fair amount of guesswork as to what you want your code to actually do, but hopefully this will at least help you find a direction.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…