samedi 11 septembre 2021

Nested if loop not working in python when renaming file

I am trying to change the file name inside my folder. My file name is "20.1.tif". I want to rename this in "20.1". The second number .1. goes till 7 like 20.7 and the first number is till 280. i tried looping through the files and renaming it but its not looping through the first number. It goes "20.8".And as there is no 20.8 it through file not found error

 import os
    a = 20
    b = 1
    while True:
        old_file = os.path.join("D:\\file\\{}.{}.tif.tif").format(a,b)
        new_file = os.path.join("D:\\file\\{}.{}.tif").format(a,b)
        os.rename(old_file, new_file)
        b += 1
        if b > 7:
            a += 1
            if a > 280:
                break

Aucun commentaire:

Enregistrer un commentaire