title: How to change folder names in python?
body: <p>I have multiple folders each with the name of a person, with the first name(s) first and the surname last.  I want to change the folder names so that the surname is first followed by a comma and then the first name(s) follow.</p>
  
  <p>As an example, in the folder <strong>Test</strong>, i have:</p>
  
  <pre><code>C:/Test/John Smith
  C:/Test/Fred Jones
  C:/Test/Ben Jack Martin
  </code></pre>
  
  <p>and i want to make this:</p>
  
  <pre><code>C:/Test/Smith, John
  C:/Test/Jones, Fred
  C:/Test/Martin, Ben Jack
  </code></pre>
  
  <p>I tried some things with os.rename but i couldn't seem to make it work with the varying name length, and i wasn't sure how to insert the comma into the surname.</p>
  
  <p>Also, some of the folder names are already in the correct form, so i need to skip these folders during the renaming.  I think you can do this by just adding an if, so that if the folder name contains a comma it will continue.</p>
  
  <p>Otherwise, the surname will always be the last word in the folder name.</p>
  
  <p>Thanks for any help you can provide.</p>