Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alekrutkowski/e351d0e9bef27b911960f3352fbc46e2 to your computer and use it in GitHub Desktop.
Save alekrutkowski/e351d0e9bef27b911960f3352fbc46e2 to your computer and use it in GitHub Desktop.
import os
import random
import string
while True:
folder_path = input("Please enter the folder path: ")
# Check if the path is a valid directory
if os.path.isdir(folder_path):
print(f"Valid directory: {folder_path}")
break # Exit the loop if the path is valid
else:
print("The entered path is not a valid directory. Please try again.")
def generate_unique_filename(existing_filenames):
"""Generate a unique filename in the format of four capital letters followed by four digits."""
while True:
filename = ''.join(random.choices(string.ascii_uppercase, k=4)) + ''.join(random.choices(string.digits, k=4)) + '.jpg'
if filename not in existing_filenames:
return filename
def rename_jpg_files(folder_path):
"""Rename all JPG files in the specified folder to a unique name format."""
existing_filenames = set() # To track generated filenames and ensure uniqueness
for filename in os.listdir(folder_path):
if filename.lower().endswith('.jpg'):
unique_filename = generate_unique_filename(existing_filenames)
existing_filenames.add(unique_filename)
original_path = os.path.join(folder_path, filename)
new_path = os.path.join(folder_path, unique_filename)
os.rename(original_path, new_path)
print(f"Renamed '{filename}' to '{unique_filename}'")
rename_jpg_files(folder_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment