Okay, well I have to say I am completely stumped where to go from here. I am writing a media player and I am currently working on song indexing. I have figured out how to extract the song name and its complete path using os.walk(); however, I have no idea how I would find the artist or album of the song. I have an idea, but let me show you my code first.

#!/usr/bin/python

from os import walk
from os.path import join
from os.path import splitext

class Indexer:
    def __init__(self):
        self.allFiles = {}
        for base, directoryList, fileList in walk("/home/foo/Music"):
            if fileList != []: #Make sure the entry isn't empty
                for file in fileList:
                    for acceptedEnding in [".mp3", ".wav"]:
                        if splitext(file)[1] == acceptedEnding:
                            fullPath = join(base, file)
                            self.allFiles[file] = fullPath
    def returnFiles(self):
        return self.allFiles
    
myIndexer = Indexer()
fileDict = myIndexer.returnFiles()

So let me give a possible idea if anybody could tell me how to do this. Let's say you have a directory such as so: "/home/foo/Music/". Generally, songs by "The Eagles" in the album "Hell Freezes Over" would be located in the directory "/home/foo/Music/The Eagles/Hell Freezes Over/<songname>". How could I extract the "/The Eagles/Hell Freezes Over" part in order to get the artist and album?

Or if anybody has a better/more efficient method, that would be great!

Thanks in advance.

EDIT: I am of course still learning, and I noticed that my code is not quite easy on the eyes. Any ideas how I could improve its readability?

If you can get the whole string (which you can with os.walk) then you can do this no worries :)

>>> s = "/home/foo/Music/The Eagles/Hell Freezes Over/<songname>"
>>> list_s = s.split('/')
>>> list_s
['', 'home', 'foo', 'Music', 'The Eagles', 'Hell Freezes Over', '<songname>']
>>> print list_s[-2]
Hell Freezes Over
>>> print list_s[-3]
The Eagles
>>>

See how this works? We split up the path of the song, then if we know how it is structured then we can extract the song album and artist. :)

Have a fiddle, see if you can incorporate that into your own program :)

EDIT: Thats an awesome album by the way

what if do we search for multiple songs?

your approch ıs good but ıt ıs just for one file(song)

thanks for the answers

If you do multiple songs then to play them you would need the path to every song so therefore my method would work fine, you would just use my method for every song that you needed to find out the info.

@paul: Sorry for the late reply, I fell asleep at the keyboard. Anyways, thanks for the reply. That's exactly what I wanted.

@rehber: Okay, well I implemented Paul's idea and now I have it working flawlessly. Here's the full working code if you want it.

#!/usr/bin/python

from os import walk
from os.path import join
from os.path import splitext

class Indexer:
    def __init__(self, targetDir):
        self.allFiles = {}
        for base, directoryList, fileList in walk(targetDir):
            if fileList != []:
                for file in fileList:
                    for acceptedEnding in [".mp3", ".wav"]:
                        if splitext(file)[1] == acceptedEnding:
                            file = file.replace(acceptedEnding, "")
                            fullPath = join(base, file)
                            pathParts = fullPath.split("/")
                            artist = pathParts[-3]
                            album = pathParts[-2]
                            self.allFiles[file] = (fullPath, artist, album)
    def returnFiles(self):
        return self.allFiles
    
myIndexer = Indexer("/home/pat/Music")
allSongs = myIndexer.returnFiles()

for song in allSongs:
    print "Song name =", song
    print "Song path =", allSongs[song][0]
    print "Artist name =", allSongs[song][1]
    print "Album name =", allSongs[song][2]
    print

Thanks again Paul.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.