472,146 Members | 1,379 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,146 software developers and data experts.

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

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
5 1539
ilikepython
844 Expert 512MB
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
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
844 Expert 512MB
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
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
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.

Similar topics

16 posts views Thread by dudufigueiredo | last post: by
4 posts views Thread by _BNC | last post: by
11 posts views Thread by globalrev | last post: by
5 posts views Thread by kumarboston | last post: by
2 posts views Thread by rotaryfreak | last post: by
reply views Thread by Saiars | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.