Created
July 29, 2023 11:03
-
-
Save TanjimReza/9533c84572b14ec653306d76aa9dec4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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