'Convert JFIF files to JPEG in python

I have a folder of folders with JFIF photos and I need to convert them all to JPEG format, I think the problem is coming from either my if statement because there are other photos in the folders that do not need to be converted or the for loop. Thank you

from PIL import Image
import os

root = r"(my computer path to the folder)"

count = 0
for dirs, subdir, files in os.walk(root):
    for file in files:
        lastChar = file[-1:]
        if(lastChar == 'f'):
            img = Image.open(file)
            #file ends in .jfif, remove 4 characters
            fileName = file[:-4]
            #add jpg and save
            img.save(fileName + "jpg")

I am getting this error right now, FileNotFoundError: [Errno 2] No such file or directory: 'IMG_4242_1615254307.jfif'



Solution 1:[1]

from PIL import Image
import os

root = r"my computer path to the folder"
print(root)
count = 0
for dirs, subdir, files in os.walk(root):
    for file in files:
        lastChar = file[-4:]
        if(lastChar == 'jfif'):
            img = Image.open(root+'\\'+file)
            #file ends in .jfif, remove 4 characters
            fileName = file[:-4]
            print(fileName)
            #add jpg and save
            img.save(root+'\\'+fileName + "jpg")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1