Skip to main content
1 of 3

The structure of these if else all similar, you can use re to simplify it.

if "IMG_" in filename:
    os.rename(filename, filename.replace('IMG_', ''))
    number_modified_files +=1
elif "PANO_" in filename:
    os.rename(filename, filename.replace('PANO_', ''))
    number_modified_files +=1
elif "VID_" in filename:
    os.rename(filename, filename.replace('VID_', ''))   
    number_modified_files +=1

So you are looking for IMG_, PANO_ and VID_ in filename and try to replace it delete this part.

Instead of using os.rename multiply times, we can use re.sub(pattern, repl, string, count=0, flags=0) to do this.

It will Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)

The pattern meaning match one in three. I am not sure if your are familiar with regex, here is the doc.

And if the renamed_filename not equal filename it is modified, so whole part will be

pattern = 'IMG_|PANO_|VID_'
renamed_filename = re.sub(pattern, '', filename)
if renamed_filename != filename:
    number_modified_files +=1
    os.rename(filename, renamed_filename)