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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BurnTard
    New Member
    • May 2007
    • 52

    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?

    [Code=python]


    for n in range(1000)


    bandname=raw_in put("Band Name? ")
    folderpath=raw_ input("The exact path to the folder in which the mp3's are? ")
    l1=[]
    l1.append(filen ames in folderpath)

    for item in l1:
    if item contains bandname:
    item=item-bandname


    for item in l1:
    assign item to corresponding file or summat like that


    questionthingie =raw_input("Wan na start again? ")
    if questionthingie =="Yeah":
    print "Okay, here we go"
    else:
    break

    [/Code]

    Anyone have any suggestions as to where I should go from here?
    I would greatly appreciate it ^^

    Burnie
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by BurnTard
    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?

    [Code=python]


    for n in range(1000)


    bandname=raw_in put("Band Name? ")

    l1=[]
    l1.append(filen ames in folderpath)

    for item in l1:
    if item contains bandname:
    item=item-bandname


    for item in l1:
    assign item to corresponding file or summat like that


    questionthingie =raw_input("Wan na start again? ")
    if questionthingie =="Yeah":
    print "Okay, here we go"
    else:
    break

    [/Code]

    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?:
    [code=python]
    import os, os.path
    while 1:
    bandname = raw_input("Band name: ")
    if not bandname: break
    folderpath = raw_input("The exact path in which the MP3's are located: ")

    filenames = [name for name in os.listdir(fold erpath) if os.path.splitex t(name)[1] == ".mp3"]

    for name in filenames:
    if bandname in name:
    newname = os.path.join(fo lderpath, name.replace(ba ndname, ""))
    os.rename(os.pa th.join(folderp ath, name), newname)

    [/code]
    Is that what you need?

    Comment

    • BurnTard
      New Member
      • May 2007
      • 52

      #3
      Originally posted by ilikepython
      I'm not really sure what you want to do. Do you want to rename all files with a band name without the bandname?:
      [code=python]
      import os, os.path
      while 1:
      bandname = raw_input("Band name: ")
      if not bandname: break
      folderpath = raw_input("The exact path in which the MP3's are located: ")

      filenames = [name for name in os.listdir(fold erpath) if os.path.splitex t(name)[1] == ".mp3"]

      for name in filenames:
      if bandname in name:
      newname = os.path.join(fo lderpath, name.replace(ba ndname, ""))
      os.rename(os.pa th.join(folderp ath, name), newname)

      [/code]
      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.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by BurnTard
        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.
        [code=python]
        import os, os.path
        [/code]
        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
        [code=python]
        while 1:
        bandname = raw_input("Band name: ")
        if not bandname: break
        folderpath = raw_input("The exact path in which the MP3's are located: ")
        [/code]
        Next, you get all the filenames from the directory:
        [code=python]
        filenames = [name for name in os.listdir(fold erpath) if os.path.splitex t(name)[1] == ".mp3"]
        [/code]
        That line could be translated into this:
        [code=python]
        filenames = []
        for name in os.listdir(fold erpath): # listdir return a list of files in the directory
        if os.path.splitex t(name)[1] == ".mp3": # only files with extension ".mp3"
        filenames.appen d(name) # add to list
        [/code]
        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:
        [code=python]
        for name in filenames: # iterate over files
        if bandname in name: # check if the bandname is in the filename
        newname = os.path.join(fo lderpath, name.replace(ba ndname, "")) # os.path.join joins 2 directories; join the path and the name; # replace the bandname with an empty string
        os.rename(os.pa th.join(folderp ath, name), newname) # rename using the os module
        [/code]
        Does that make sense?

        Comment

        • BurnTard
          New Member
          • May 2007
          • 52

          #5
          Originally posted by ilikepython
          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!

          Comment

          • BurnTard
            New Member
            • May 2007
            • 52

            #6
            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:\documen ts and settings\Bj\xf8 rn 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...

            Comment

            Working...