-3

I've got a folder with many (around 500) files, named like
roz001.mp3
roz002.mp3
...

Then I've got a .txt (called "names.txt") file formatted like
roz001 O korytnačke a opici
roz002 Vlk a líška
...

Basically, it goes like "(filename without extension)(space)(string with desired filename without extension)".

How can I rename these files using this list? I need to rename file "roz001.mp3" to "O korytnačke a opici.mp3" and so on. I don't really care how will this be accomplished - I have access to a terminal (I'm on a Mac) and I can also use Python, but I'm willing to use any means that can be used to do this.

Thank you in advance for all and any help.

tinnyx
  • 13
  • 2
  • 1
    Python could to that easily. What have you tried ? – Serge Ballesta Jul 31 '15 at 12:05
  • I tried to go by [this](http://stackoverflow.com/questions/2759067/rename-files-in-python) thread. I thought about loading up the file list into a dictionary, where the original filename would be the key and desired name would be the value and then go through all the files in the folder, split the name of file I'm currently renaming by ".mp3", look if the first part of original filename is listed as a key in my dictionary and if yes, replace it with value for that key in dictionary. But I got stuck at the beginning - I could't figure out how to load up the name list into dictionary... – tinnyx Jul 31 '15 at 12:14
  • The thread you cite browse the whole directory. Here it is simpler : just read "names.txt" and process each line ... And you should give the reference to the other thread in the question and say there what you wanted to do. I could avoid some more downvotes for lack of previous efforts ... – Serge Ballesta Jul 31 '15 at 12:32

2 Answers2

2

In Python, it will be quite simple :

  • open the file "names.txt" and read it line by line
  • for each line split in on first space in old_name and new_name
  • rename old_name.mp3 to new_name.mp3

Code could be like :

import os

with open("names.txt") as fd:
    for line in fd:
        line = line.strip()
        if len(line) == 0: continue
        old, new = line.strip().split(" ", 1)
        os.rename(old.strip() + ".mp3", new.strip() + ".mp3")
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Thank you so much. I ended up adding `if os.path.isfile(old.strip() + ".mp3"):` before `os.rename()` since my list wasn't bulletproof as I found out when it crashed at me for a few times, but this really did solve my question. Really, you saved me, thanks. – tinnyx Jul 31 '15 at 13:05
0

take a look here(Bulk Rename), think it will do what you need to do.

Another option is to write a batch script that uses the for /f loop (explained here)

Hope this helps

Dave
  • 9
  • 2