0

I have two lists: songs is a list of song titles, filenames is a list of song MP3 files that is generated by running os.listdir().

songs = ['The Prediction', 'Life We Chose', 'Nastradamus', 'Some of Us Have Angels', 'Project Windows', 'Come Get Me', "Shoot 'em Up", 'Last Words', 'Family', 'God Love Us', 'Quiet Niggas', 'Big Girl', 'New World', 'You Owe Me', 'The Outcome']

Each song is unicode.

filenames = ['Nas - Big Girl.mp3', 'Nas - Come Get Me.mp3', 'Nas - God Love Us.mp3', 'Nas - Life We Chose.mp3', 'Nas - Nastradamus.mp3', 'Nas - New World.mp3', "Nas - Shoot 'Em Up.mp3", 'Nas - Some of Us Have Angels.mp3', 'Nas - The Outcome.mp3', 'Nas - The Prediction.mp3', 'Nas Feat. Bravehearts - Quiet Niggas.mp3', 'Nas Feat. Ginuwine - You Owe Me.mp3', 'Nas Feat. Mobb Deep - Family.mp3', 'Nas Feat. Nashawn - Last Words.mp3', 'Nas Feat. Ronald Isley - Project Windows.mp3']

Each filename is a string.

I want to be able to look at the songs list, if one of the items from the songs list matches inside the filenames list, rename the file to that of the song.

9
  • 1
    Yes, that makes sense. What have you tried? Commented Feb 18, 2017 at 21:23
  • 1
    No, it doesn't make sense. What do you expect as a result? Describe the variables you'll end up with as well as the filenames that folder should contain. Commented Feb 18, 2017 at 21:25
  • 1
    It looks like you're just trying to remove the artist name from some mp3s, in which case you could just use something like ID3-TagIt and generate new filenames from the ID3 tags. As a bonus, that'll prevent errors like "Shoot 'em Up" vs "Shoot 'Em Up" from cropping up. Commented Feb 18, 2017 at 21:42
  • 1
    Possible duplicate of Iterate through two lists, check for matches and then rename Commented Feb 18, 2017 at 21:53
  • 1
    Do you not care about "Nas - Shoot 'Em Up.mp3" not being changed? Because none of the posted answers take that into account. Commented Feb 19, 2017 at 1:35

2 Answers 2

1

Here is a version that will maintain the file extension, whatever it was, and will avoid that the same filename is matched twice by deleting it from the filenames array after a match. It also is case insensitive:

for song in songs:
    for i, filename in enumerate(filenames):
        if song.upper() in filename.upper():
            os.rename(filename, song + os.path.splitext(filename)[1])
            del filenames[i]
            break

You could also loop first over the file names, but then the problem can also be that two file names match with the same song, and the rename operation will raise an error on the second. So in that set up you'd better delete the song from the songs list once it has been matched with a file name.

Sign up to request clarification or add additional context in comments.

3 Comments

Excellent point! Never thought of that. I marked yours as the answer. Thank you!
You're welcome ;-) Not sure why someone downvoted this, but I have also added .upper() to make the comparisons case insensitive.
I didn't downvote. I have a good idea of who this is, since the guy systematically downvotes valid answers to "easy" questions, thinking it's easy rep and downvoted all the working answers here. Buuuuuut it's not working. Check this out. +1
0

After having defined the lists as you did, you want to iterate through each file and then check it against every song name. It should look something like this:

for f in filenames:
    for s in songs:
        if s in f:
            os.rename(f,s+".mp3")

This way, the file stays with an mp3 extension as well.

4 Comments

This is the best and most straight forward answer thus far. Thank you!
it's been a long time since I didn't see a first post that is properly explained and formatted, +1.
@Jean-FrançoisFabre Thanks a lot! :)
de nada. keep doing that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.