Skip to content

Instantly share code, notes, and snippets.

@buckbaskin
Created April 18, 2017 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buckbaskin/cfc424dde56981a9ab4b7aeaa8436f84 to your computer and use it in GitHub Desktop.
Save buckbaskin/cfc424dde56981a9ab4b7aeaa8436f84 to your computer and use it in GitHub Desktop.
A Python script that traverses an organized iTunes Music directory and enumerates the artists and albums in the library.
import os
def traverse():
artists = None
for dirpath, dnames, fnames in os.walk("./Music/"):
if artists is None and len(dnames) > 0:
artists = {el:[] for el in dnames}
elif len(dnames) > 0:
head, tail = os.path.split(dirpath)
if tail not in artists:
artists[tail] = []
artists[tail].extend(dnames)
list_artists = list(artists.iteritems())
list_artists.sort(key=lambda tup: tup[0].lower())
for artist, albums in list_artists:
if len(albums) > 0:
print 'a:', artist
for title in albums:
print ' -', title
if __name__ == '__main__':
traverse()
@buckbaskin
Copy link
Author

Example output:

a: Weird Al Yankovic
  - Mandatory Fun
  - Straight Outta Lynwood
  - The Essential Weird Al Yankovic
a: Wet Wet Wet
  - Unknown Album
a: Whitesnake
  - Whitesnake
a: Will Smith
  - Big Willie Style
a: Williams & McKnight
  - Unknown Album
a: Willie Nelson
  - Unknown Album
a: Willie Nelson & J Iglesias
  - Unknown Album
a: WIN WIN
  - Primaries
a: Yeah Yeah Yeahs
  - Fever To Tell (Parental Advisory)
a: Yeasayer
  - Fragrant World
a: Yellowcard
  - Lights And Sounds
  - Ocean Avenue
  - One For The Kids
  - Paper Walls
  - Southern Air
  - When You're Through Thinking, Say Yes
a: Young the Giant
  - My Body - Single
a: Zedd
  - Clarity
  - Clarity (feat. Foxes) - Single
a: ZZ Top
  - Eliminator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment