Over a million developers have joined DZone.

Python Id3 Tag From Containing Folder

DZone's Guide to

Python Id3 Tag From Containing Folder

· ·
Free Resource
// Total noob python script for renaming the 'album' tag in an untagged mp3 file.  I plug it into podnova (using advanced -> run command) as it downloads my podcasts so that they get sorted correctly when I copy them onto my ipod.

PodNova organizes podcasts by folder.  the iPod organizes podcasts by album tag.  If you use GtkPod to copy podcasts from PodNova, this can help keep mp3 podcasts categorized properly.

import sys

# requires ID3 module, easily googled
from ID3 import *
for arg in sys.argv:
    fullfilename = arg

# This only works for mp3 files, I would love suggestions for mp4 tags
id3info = ID3(fullfilename)

# Print command useful for logging.
print id3info

# Check if album info exists
if not id3info.has_key('ALBUM'):
    print 'appending album tag'
    # truncate to just containing directory:
    folder = fullfilename[1:rfind(fullfilename,'/')]
    # define album based on podcast's directory
    album = (folder[rfind(folder,'/'):]).strip('/')
    id3info.album = album
    if id3info.album == album:
        print 'success!'
    print 'nothing to change'

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}