import os import re # The directory path directory = r"\\vgisfile\gis\projects\Leadership\Elections\Print_Request_Maps_Julia\Deliverables\Individual" # Check if directory exists if not os.path.exists(directory): print(f"Error: Directory '{directory}' does not exist.") else: print(f"Scanning directory: {directory}") # List all files in the directory for filename in os.listdir(directory): # Skip directories if os.path.isdir(os.path.join(directory, filename)): continue # Check if the filename matches the pattern "Precincts Map Series 11x17_XXX" if filename.startswith("Precincts Map Series 11x17_"): # Extract the precinct number using regex match = re.search(r'_(\d+)', filename) if match: precinct_number = match.group(1) # Create the new filename format: Precinct_101_11x17 _, extension = os.path.splitext(filename) new_filename = f"Precinct_{precinct_number}_11x17{extension}" # Full paths old_path = os.path.join(directory, filename) new_path = os.path.join(directory, new_filename) # Rename the file try: os.rename(old_path, new_path) print(f"Renamed: {filename} -> {new_filename}") except Exception as e: print(f"Error renaming {filename}: {e}") print("File renaming complete.")