Skip to content

Instantly share code, notes, and snippets.

@TanjimReza
Created July 29, 2023 11:03
Show Gist options
  • Save TanjimReza/9533c84572b14ec653306d76aa9dec4d to your computer and use it in GitHub Desktop.
Save TanjimReza/9533c84572b14ec653306d76aa9dec4d to your computer and use it in GitHub Desktop.
import os
input_folder = r"C:\Users\Tanjim\Desktop\main\main"
# Find all sub folders
for subfolder in os.listdir(input_folder):
print(subfolder)
# Find all images in the subfolder
images = os.listdir(os.path.join(input_folder, subfolder))
print(images)
# rename all the images in the subfolder with the subfolder name and a number
for i, image in enumerate(images):
try:
os.rename(os.path.join(input_folder, subfolder, image), os.path.join(input_folder, subfolder, subfolder + "_" + str(i) + ".jpg"))
except:
print("Error in renaming the file: {}".format(os.path.join(input_folder, subfolder, image)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment