By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,609 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,609 IT Pros & Developers. It's quick & easy.

Tired of renaming mp3's? I am...

P: 52
Hi guys. It's been ages since I tried making anything halfway useful in python, and back when I did, I was almost never successful...

Anyways, the thing is: I'm finally finished moving my mp3's from a great heap in a folder called "miscellany", and I'm dead tired of organizing stuff that should have been organized ages ago, and have already been organized on my laptop...
What is the matter at the moment is that most mp3's, ripped from cd's or acquired by other means, come with a free but annoying band-name at the beginning. So what did I do? I undusted python of course, and found myself sorely incompetent... This is roughly what I want... I take it you get the picture?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. for n in range(1000)
  4.  
  5.  
  6.     bandname=raw_input("Band Name? ")
  7.     folderpath=raw_input("The exact path to the folder in which the mp3's are? ")
  8.     l1=[]
  9.     l1.append(filenames in folderpath)
  10.  
  11.     for item in l1:
  12.         if item contains bandname:
  13.             item=item-bandname
  14.  
  15.  
  16.     for item in l1:
  17.         assign item to corresponding file or summat like that
  18.  
  19.  
  20.     questionthingie=raw_input("Wanna start again? ")
  21.     if questionthingie=="Yeah":
  22.         print "Okay, here we go"
  23.     else:
  24.         break
  25.  
  26.  
Anyone have any suggestions as to where I should go from here?
I would greatly appreciate it ^^

Burnie
Aug 6 '07 #1
Share this Question
Share on Google+
5 Replies

ilikepython
Expert 100+
P: 844
Hi guys. It's been ages since I tried making anything halfway useful in python, and back when I did, I was almost never successful...

Anyways, the thing is: I'm finally finished moving my mp3's from a great heap in a folder called "miscellany", and I'm dead tired of organizing stuff that should have been organized ages ago, and have already been organized on my laptop...
What is the matter at the moment is that most mp3's, ripped from cd's or acquired by other means, come with a free but annoying band-name at the beginning. So what did I do? I undusted python of course, and found myself sorely incompetent... This is roughly what I want... I take it you get the picture?

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. for n in range(1000)
  4.  
  5.  
  6.     bandname=raw_input("Band Name? ")
  7.  
  8.     l1=[]
  9.     l1.append(filenames in folderpath)
  10.  
  11.     for item in l1:
  12.         if item contains bandname:
  13.             item=item-bandname
  14.  
  15.  
  16.     for item in l1:
  17.         assign item to corresponding file or summat like that
  18.  
  19.  
  20.     questionthingie=raw_input("Wanna start again? ")
  21.     if questionthingie=="Yeah":
  22.         print "Okay, here we go"
  23.     else:
  24.         break
  25.  
  26.  
Anyone have any suggestions as to where I should go from here?
I would greatly appreciate it ^^

Burnie
I'm not really sure what you want to do. Do you want to rename all files with a band name without the bandname?:
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2. while 1:
  3.     bandname =  raw_input("Bandname: ")
  4.     if not bandname: break
  5.     folderpath = raw_input("The exact path in which the MP3's are located: ")
  6.  
  7.     filenames = [name for name in os.listdir(folderpath) if os.path.splitext(name)[1] == ".mp3"]
  8.  
  9.     for name in filenames:
  10.         if bandname in name:
  11.             newname = os.path.join(folderpath, name.replace(bandname, ""))
  12.             os.rename(os.path.join(folderpath, name), newname)
  13.  
  14.  
Is that what you need?
Aug 7 '07 #2

P: 52
I'm not really sure what you want to do. Do you want to rename all files with a band name without the bandname?:
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2. while 1:
  3.     bandname =  raw_input("Bandname: ")
  4.     if not bandname: break
  5.     folderpath = raw_input("The exact path in which the MP3's are located: ")
  6.  
  7.     filenames = [name for name in os.listdir(folderpath) if os.path.splitext(name)[1] == ".mp3"]
  8.  
  9.     for name in filenames:
  10.         if bandname in name:
  11.             newname = os.path.join(folderpath, name.replace(bandname, ""))
  12.             os.rename(os.path.join(folderpath, name), newname)
  13.  
  14.  
Is that what you need?
I think this is what I need, but I don't quite understand it. Could you explain it line by line? What I need is to make it rename the files so that if the bandname is in the filename, it removes the bandname from the filename.
Aug 7 '07 #3

ilikepython
Expert 100+
P: 844
I think this is what I need, but I don't quite understand it. Could you explain it line by line? What I need is to make it rename the files so that if the bandname is in the filename, it removes the bandname from the filename.
Of first you import the os and os.path modules. They help with renaming the files and searching the directory.
Expand|Select|Wrap|Line Numbers
  1. import os, os.path
  2.  
Then you have the main loop. Here you ask the user for the band name and the folderpath. If the user doesn't give a bandname, the loop exits
Expand|Select|Wrap|Line Numbers
  1. while 1:
  2.     bandname =  raw_input("Bandname: ")
  3.     if not bandname: break
  4.     folderpath = raw_input("The exact path in which the MP3's are located: ")
  5.  
Next, you get all the filenames from the directory:
Expand|Select|Wrap|Line Numbers
  1.    filenames = [name for name in os.listdir(folderpath) if os.path.splitext(name)[1] == ".mp3"]
  2.  
That line could be translated into this:
Expand|Select|Wrap|Line Numbers
  1. filenames = []
  2. for name in os.listdir(folderpath):    # listdir return a list of files in the directory
  3.     if os.path.splitext(name)[1] == ".mp3":   # only files with extension ".mp3"
  4.         filenames.append(name)   # add to list
  5.  
Now, all that's left is to rename the files. You check if bandname is in the filename for every file in filenames and if it is you remove it:
Expand|Select|Wrap|Line Numbers
  1.     for name in filenames:   # iterate over files
  2.         if bandname in name: # check if the bandname is in the filename
  3.             newname = os.path.join(folderpath, name.replace(bandname, ""))  # os.path.join joins 2 directories; join the path and the name; # replace the bandname with an empty string
  4.             os.rename(os.path.join(folderpath, name), newname) # rename using the os module
  5.  
Does that make sense?
Aug 7 '07 #4

P: 52
Does that make sense?
That makes, if not perfect, a lot of sense! Thank you a lot, this was a great help! You guys just keep on impressing me =P

Again thanks!
Aug 13 '07 #5

P: 52
Now the program seems to be running along smoothly, though there is one small problem. I'm from Norway, and when I first started using this computer, I did a very stupid thing... I called myself by my real name, which unfortunately contains an norwegian letter, "". Is there any way to make python understand this letter?

At the moment, it can't change a single name, because it runs into this cursed letter before it even gets that far... For now, all the program does is inform me that it cant find the folder: "c:\documents and settings\Bj\xf8rn Magne\min musikk\musikk\", which really us no wonder, because my name isn't Bj\xf8rn Magne, no matter how much I want it to be...

Anyone help? This problem is starting to annoy me...
Aug 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.