reading id3 tags with python

J

jeff

i stated using a python module called id3reader
(http://www.nedbatchelder.com/code/modules/id3reader.html) and i tried
to use it to organize all my music files (right now they are all in one
folder (i want them in Music/artist/album/title.mp3)) but im getting an
error with the code, however it does seem to be from the id3 library,
and not from /my/ code, so if somebody knows how to fix this problem,
or even if somebody knows of a better id3 library please tell me.

my code::
#############################################
#! /usr/bin/python
import id3reader as id3
from glob import *
import sys, os
#from shutil import *

if len(sys.argv) >= 2:
location = sys.argv[1]
else:
location = '../'

files = glob(location + "*.mp3")

print "\n\n"
print "Welcome to the MP3 renamer"
print "files following this pattern will be changed::"
print location + "*.mp3"
print str(len(files)) + " will be affected"
Continue = True
while Continue == True:
x = raw_input("\tContinue?(y/n)")
if x=='y' or x=='n':
if x=='y':
print "continueing..."
Continue = False
#################################
for file in files:
info = id3.Reader(file)
data = {}
data['artist'] = info.getValue('performer')
if data['artist'] == None:
data['artist'] = "Unknown Artist"
data['album'] = info.getValue('album')
if data['album'] == None:
data['album'] = "Unknown Album"
data['title'] = info.getValue('title')

try:
os.mkdir(data['artist'])
os.mkdir(data['artist'] + '/' + data['album'])
except OSError:
print "directory already exists...continueing anyways"

#print data
new_file = data['artist'] + '/' + data['album'] + '/' + data['title']
+ '.mp3'
os.rename(file,new_file)

print "\n\n"
#############################################
for those of you who dont understand this, ill try to explain my best
here::
lines::
1) shebang (telling linux to use python)
2-5) imports (shutils was commented out -- i was using this before, but
now im not)
7-10) finding out the directory to find the music in
12) getting a tuple of all the files in the directory it's looking in
with the extension mp3
14-25) making sure you realy want to do this
27-end) for each file::
27-36) getting all the id3 info and storing it in a dictionary
38-42) tries to make the directories (try will make sure no error is
shown if they already exist)
45-46) renames he file, into the new directory
 
D

Dennis Lee Bieber

i stated using a python module called id3reader
(http://www.nedbatchelder.com/code/modules/id3reader.html) and i tried
to use it to organize all my music files (right now they are all in one
folder (i want them in Music/artist/album/title.mp3)) but im getting an
error with the code, however it does seem to be from the id3 library,
and not from /my/ code, so if somebody knows how to fix this problem,
or even if somebody knows of a better id3 library please tell me.

my code::
said:
Continue = True
while Continue == True:
x = raw_input("\tContinue?(y/n)")
if x=='y' or x=='n':
if x=='y':
print "continueing..."
Continue = False

Pardon? The above is a meaningless loop... If one answers with "n"
the while loop will repeat, asking for "y/n"... The only way out of that
loop is by answering "y" (you don't account for upper case, or someone
typing "yes"/"no"

And is indentation correct?

while True:
ans = raw_input("\nContinue (y/n)? ").lower()
if ans.startswith("n") or ans.startswith("y"):
break
else:
print "Unknown response: %s" % ans

if ans.startswith("y"):
print "... continuing"
#put rest of logic inside this if block...
#that way a "n" response will just go to the end and exit

#############################################
for those of you who dont understand this, ill try to explain my best
here::

<snip>

So... to quote Clara Pellar: "Where's the beef?"

That is, WHAT ERROR are you getting? Show us the traceback
(cut&paste, don't retype from another window as you might miss something
critical)

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
L

LaundroMat

Heh, a description of the error would be nice indeed.

Just a preliminary warning: with this code you will also be parsing
directories. id3reader can't handle those ofcourse.

Better add a check such as eg:
if os.path.isfile(os.path.join(directory, file)):
# do your thing

laundro
 
J

jeff

well, heres the error::
######
Traceback (most recent call last):
File "./main.py", line 28, in ?
info = id3.Reader(file)
File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 187,
in __init__
self._readId3()
File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 306,
in _readId3
self._interpretFlags()
File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 341,
in _interpretFlags
self._readExtHeader = _readExtHeader_rev3
NameError: global name '_readExtHeader_rev3' is not defined
####
also, i didnt account for Y/N/Yes/No, whatever because this script is
just for myself (also why i didnt use that function to join pathnames,
i oonly realy use linux anymore)

and what do you mean by 'id3reader' cant do directories? my for loop
just does each file in the dirextory
 
R

Rabin Vincent

File "/home/jeffrey/Documents/Music/.rename/id3reader.py", line 341,
in _interpretFlags
self._readExtHeader = _readExtHeader_rev3
NameError: global name '_readExtHeader_rev3' is not defined

Add a "self." in front of _readExtHeader_rev3 on line 341 of id3reader.py,
and also in front of _readExtHeader_rev4 on line 343 and it should
probably work.

self._readExtHeader = self._readExtHeader_rev3

Rabin
 
L

LaundroMat

and what do you mean by 'id3reader' cant do directories?
my for loop just does each file in the dirextory

It's just a friendly warning that you shouldn't suppose that all that
is scanned are indeed files, and not directories.
 
J

jeff

ok, i see..nut its just for myself--im not planning on redistributing
it, and most people dont have folders that end with '.mp3' in their
music folder
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top