[Rod Stephens Books]
Index Books Python Examples About Rod Contact
[Mastodon] [Bluesky]
[Build Your Own Ray Tracer With Python]

[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

Title: Rename JPEG files to JPG files in a selected directory in Python

[Renaming JPEG files to JPG files in a selected directory in Python]

Some applications seem to still be stuck in the 1990s so they produce JPEG files and I find that annoying. If you use File Explorer to rename them to have a .jpg extension, Windows warns you that changing the extension might make them not work with other applications, which is time consuming and silly because .jpeg and .jpg files are the same.

Anyway I wrote this program to rename the JPEG files so they have the .jpg extension.

The program performs three main tasks of interest: browsing for a folder, listing .jpeg files, and renaming the files.

Browsing for a Folder

When you click the Browse button, the following code executes.

from tkinter import filedialog ... def browse_folder(self): '''Let the user browse for a folder.''' folder_path = filedialog.askdirectory(initial=self.folder_entry.get()) if folder_path: self.folder_entry.delete(0, tk.END) self.folder_entry.insert(0, folder_path)

This code calls filedialog.askdirectory to let the user select a folder. It starts browsing at the program's current working directory.

That method returns either a blank string if the user canceled the dialog or the path to the selected folder. If the result is not a blank string, the code makes the program's Folder text box display the selected folder's path.

Listing .jpeg Files

The program uses the following code snippet to create its list box.

self.file_listvariable = tk.Variable() file_listbox = tk.Listbox(self.window, listvariable=self.file_listvariable) file_listbox.pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True, padx=MARGIN)

The notable thing about this code is that it creates a tk.Variable to hold the list box's items.

When the user clicks the List button, the following code lists the JPEG files in the selected directory.

def list_files(self): '''List JPEG files.''' try: path = self.folder_entry.get() os.chdir(path) pattern = '*.jpeg' self.file_listvariable.set(glob.glob(pattern)) except Exception as ex: print(f'Error {ex}')

This code first gets the text in the Folder text box and passes it to os.chdir to set the program's working directory to the value entered. This code is inside a try except block because the user can type directly into the Folder text box and that directory may not exist.

Next, the code defines the file name pattern *.jpeg and passes it into glob.glob to search the current directory for files matching that pattern. The result is possibly empty list of file names. The code sets the list box's listvariable equal to the returned list so you can see the .jpeg files.

Renaming Files

The last piece of the program executes when you click the Rename button.

def rename_files(self): '''Rename the JPEG files.''' filenames = self.file_listvariable.get() for filename in filenames: new_filename = filename[0:-5] + '.jpg' print(f'{filename} --> {new_filename}') os.rename(filename, new_filename) self.file_listvariable.set([])

This code gets the list of file names from the list box's listvariable. It then loops through the file names, removes the .jpeg extension and appends .jpg, prints a note so you can see what it's doing, and then calls os.rename to rename the file. The method finishes by clearing the list box.

Conclusion

Fortunately most applications save files with the .jpg extension instead of .jpeg. If you do get stuck with a bunch of .jpeg files, you can use this program to rename them.

Download the example to experiment with it and to see additional details.

© 2025 Rocky Mountain Computer Consulting, Inc. All rights reserved.