473,322 Members | 1,566 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

reading id3 tags with python

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
5 4311
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Fazer | last post by:
Hello, I was wondering what would be the easiest way to strip away HTML tags from a string? Or how would I remove everything between < and > also the < , > as well using regex? Thanks for...
4
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # to open a file and write to file # do f=open('xfile.txt','w') # this creates a file "object" and name it f. # the second argument of open can be
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
4
by: supster | last post by:
I have the full file path of a file stored as a string and I'de like to get the file size in bytes. What is the most efficient way of doing this with a static method from the File class or...
7
by: emferrari | last post by:
Hi all I have a XML file with the following: <Step step="1"> <Text><b>Chapter 1</b> This is a test. Click <b><i>Load</i></b> </Text> </Step>
3
by: lizii | last post by:
i have a file - which on each line has some data i need to fill into a box - now although reading in the data is simple enough and putting it in the correct box will be no problem, as i can just...
2
by: dennis.sprengers | last post by:
Ik ben bezig met een eigen UBB editor. Als iemand aan het typen is, zorgt CTRL-B voor een \-tag en nogmaals CTRL-B voor een \ tag. Als je eerst een selectie maakt en dan CTRL-B drukt, wordt de...
3
by: Paul Moore | last post by:
I'd like to write some scripts to analyze and manipulate my music files. The files themselves are in MP3 and FLAC format (mostly MP3, but FLAC where I ripped original CDs and wanted a lossless...
0
by: Stephen Moore | last post by:
hello I have a situation where I have quite a large python object which I want to convert to yaml so I can then send it to a flex application using pyamf. I've managed to create a yaml...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.