Omitting results with id3reader [Beginner]

Chris cwitts at gmail.com
Wed Dec 5 07:16:19 EST 2007


On Dec 5, 2:04 pm, Ionis <lewisnorto... at googlemail.com> wrote:
> On Dec 5, 11:59 am, Chris <cwi... at gmail.com> wrote:
>
> > Ok, just noticed you linked the id3reader.  I tested my code and it
> > worked fine.
>
> Thanks alot Chris. Could you comment your code so I can see what each
> line is doing? I hope that isn't a problem. Still pretty new to python.

import os, fnmatch
""" fnmatch will check your directory listing and compare
    it to the pattern, in this case mp3, and will normalize
    the case for you as well and only return those matching
    the pattern.
    Equivalent commands would be: dir *.mp3 or ls *.mp3
"""
# os.listdir() and the fnmatch.filter will return you lists
# to iterate over so no need to store it seperately
for each_file in fnmatch.filter(os.listdir(path), '*.mp3'):
  id3r = id3reader.Reader(each_file)     # Read the new file
  """ A value of None will return false if you test it so
      by just doing 'if value', it will return false if
      there is nothing.
  """
  if id3r.getValue('performer') and id3r.getValue('title'):
    output = open('output.html','ab')
    # using string formatting is neater than string concatenation
    output.write('<p>%s - %s</p>\n' % (id3r.getValue('performer'),
id3r.getValue('title')) )
    output.close()

HTH,
Chris



More information about the Python-list mailing list