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

reading id3 tags with python

P: n/a
i stated using a python module called id3reader
(http://www.nedbatchelder.com/code/mo...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

Nov 24 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

Nov 24 '06 #2

P: n/a
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

Nov 24 '06 #3

P: n/a
On 24 Nov 2006 08:42:02 -0800, jeff <je****************@gmail.comwrote:
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
Nov 25 '06 #4

P: n/a
On Nov 24, 5:42 pm, "jeff" <jeffrey.ayleswo...@gmail.comwrote:
[snip]
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.

Nov 27 '06 #5

P: n/a
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
It's just a friendly warning that you shouldn't suppose that all that
is scanned are indeed files, and not directories.
Dec 8 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.