How can I rename folders to 'surname, firstname' in Python?

How can I change folder names in Python?

I have several folders, each named after a person with the first name(s) followed by the surname. I want to rename these folders so that the surname comes first, followed by a comma, and then the first name(s).

For example, I have the following folders in C:/Test:

  • C:/Test/John Smith
  • C:/Test/Fred Jones
  • C:/Test/Ben Jack Martin

And I want to rename them to:

  • C:/Test/Smith, John
  • C:/Test/Jones, Fred
  • C:/Test/Martin, Ben Jack

I tried using os.rename, but I couldn’t figure out how to handle varying name lengths or how to properly insert the comma into the surname.

Additionally, some folders are already in the correct format, so I need to skip renaming those. I think I can use an if condition to check if the folder name contains a comma and skip those folders. Otherwise, the surname should always be the last word in the folder name.

How can I achieve this renaming task in Python?

Okay, from my experience, renaming folders in Python is quite straightforward using os.rename and split. You can use this method if your folders follow a standard format (e.g., first name followed by surname). Here’s how you can do it:

import os

folder_path = "C:/Test"
for folder_name in os.listdir(folder_path):
    folder_full_path = os.path.join(folder_path, folder_name)
    
    if ',' in folder_name:
        continue  # Skip if the folder name already contains a comma
    
    # Split the name by spaces
    name_parts = folder_name.split()
    surname = name_parts[-1]
    first_names = ' '.join(name_parts[:-1])
    
    # Construct the new name
    new_name = f"{surname}, {first_names}"
    
    # Rename the folder
    new_folder_full_path = os.path.join(folder_path, new_name)
    os.rename(folder_full_path, new_folder_full_path)
    print(f"Renamed {folder_name} to {new_name}")

This should work fine for single first names, and it’s simple to use. Of course, if you’re using a version of Python where you’re dealing with Python 2to3 transitions, you’d want to ensure everything’s compatible.

Great solution, @yanisleidi-rodriguez! I’d add one more approach with regular expressions if you’re working with slightly more complex name formats. This method would work better if there are multiple first names or more specific naming conventions. Here’s a twist on what you suggested using regular expressions:

import os
import re

folder_path = "C:/Test"
for folder_name in os.listdir(folder_path):
    folder_full_path = os.path.join(folder_path, folder_name)

    if ',' in folder_name:
        continue  # Skip if the folder name already contains a comma
    
    # Match the first name(s) and surname
    match = re.match(r"(.+) (\w+)$", folder_name)
    
    if match:
        first_names = match.group(1)
        surname = match.group(2)
        
        # Create new name in "Surname, Firstnames" format
        new_name = f"{surname}, {first_names}"
        new_folder_full_path = os.path.join(folder_path, new_name)
        
        # Rename the folder
        os.rename(folder_full_path, new_folder_full_path)
        print(f"Renamed {folder_name} to {new_name}")

This version helps if your naming follows the first name(s) followed by the surname. It’s flexible and can be tweaked for more complex names as well. Just a heads up, though, if you’re using Python 2to3, be sure to check the regex syntax compatibility between Python 2 and Python 3

Nice one, @yanisleidi-rodriguez! I like how you’ve used regular expressions, but I thought I’d throw in a variant that better handles multiple first names. This one works well when you’re dealing with names that have more than one first name before the surname:

import os

folder_path = "C:/Test"
for folder_name in os.listdir(folder_path):
    folder_full_path = os.path.join(folder_path, folder_name)
    
    if ',' in folder_name:
        continue  # Skip if the folder name already contains a comma
    
    # Split the folder name into components
    name_parts = folder_name.split()
    surname = name_parts[-1]  # Last word is the surname
    first_names = ' '.join(name_parts[:-1])  # All words before the surname are the first names
    
    # Create the new folder name
    new_name = f"{surname}, {first_names}"
    
    # Rename the folder
    new_folder_full_path = os.path.join(folder_path, new_name)
    os.rename(folder_full_path, new_folder_full_path)
    print(f"Renamed {folder_name} to {new_name}")

This method handles multi-part first names well, and it ensures that your folders follow the ‘surname, firstname’ format regardless of how many first names you have. Just remember, if you’re transitioning from Python 2to3, ensure your code runs smoothly across both versions