-2

Im a beginner to python and I need to make a script that renames file names.

My files are in the format Name Surname but I need them in Surname Name

I have files that are like this:

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

and I need them like this:

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

I have tried multiple things with os.rename but I really cant get it to work.

Thanks for the help in advance.

Ilko1810
  • 7
  • 4
  • 2
    What have you tried so far? Post your code and the error you are getting. – Lomtrur Jul 01 '19 at 09:52
  • I have deleted the code with anger. Could you tell me the steps I should approach so I research my way into it? – Ilko1810 Jul 01 '19 at 09:53
  • [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python) – Lomtrur Jul 01 '19 at 09:55
  • 2
    Possible duplicate of [How to rename a file using Python](https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python) – Lomtrur Jul 01 '19 at 09:55
  • Those look like general renaming guides. – Ilko1810 Jul 01 '19 at 09:59
  • That is how you rename files. Without any specifics why that does not work for you no one can help you. Try again, edit in your code and the error. – Lomtrur Jul 01 '19 at 10:01
  • but the problem is. How do I go on of changing my code to something that would allow me to rename files in that way. I know how to rename from 1 thing to another. – Ilko1810 Jul 01 '19 at 10:03
  • use the below code in your program with proper file path. – Arun Augustine Jul 01 '19 at 10:06
  • Take the file name, `.split()` and merge the two parts in different order (`split_name = file_name.split(); new_file_name = split_name[1]+" "+split_name[0]` for two-part names) – h4z3 Jul 01 '19 at 10:09
  • I will try this. Thank you. – Ilko1810 Jul 01 '19 at 10:14

4 Answers4

2
  1. Split filename at the last space using rsplit (assuming a last name is a single word)
  2. Reverse and rejoin to get <name> <lastname>
  3. Rebuild a new file name and rename the file.

Also use pathlib package for file operations.

from pathlib import Path

if __name__ == '__main__':
    source_dir = Path('C:/Test')
    for file in source_dir.glob('**/*'):
        if file.is_dir():
            continue
        name_parts = file.stem.rsplit(maxsplit=1)
        name_reversed = ' '.join(name_parts[::-1])
        new_path = file.with_name(name_reversed + file.suffix)
        # check the result
        print(file, new_path)
        # rename
        # file.rename(new_path)


abdusco
  • 9,700
  • 2
  • 27
  • 44
0

Try this,

import os
path = "C:/Test/Smith John"
os.rename(re.search('Test\/(.+?)$', path).group(1), v.split(' ')[1]+" "+v.split(' ')[0])
Arun Augustine
  • 1,690
  • 1
  • 13
  • 20
  • There are 1000 files like this. I need a code that would change all of them. There are lots of Name Surname files which need to be changed to Surname Name. – Ilko1810 Jul 01 '19 at 10:08
0
import os

# change working directory to the (C:/Test/)
os.chdir(“C:/Test/“)
os.rename(“Smith John”, “John Smith”)
os.rename(“Jones Fred”, “Fred Jones” )
os.rename(“Jack Martin Ben”, “Ben Jack Martin”)
Reema
  • 1
  • 2
  • Please add explanation around your code. So that OP and future reader can easily understand your answer. – Sangam Belose Jul 01 '19 at 10:56
  • You should have read [this comment](https://stackoverflow.com/questions/56833498/how-to-rename-multiple-files-in-python#comment100219455_56833498) before posting your answer – barbsan Jul 01 '19 at 11:08
0

Try this: You can modify the file name by searching the particular string and reverse the file name.

path = "C:/Test/"
entries = os.listdir(path)
   for entry in entries:
       old_filename = re.search('(.+?)$', entry).group(1)
       new_filename = ' '.join(old_str.split()[-1::-1])
       modified_path = path+new_filename
       print(modified_path)

O/p:

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