Search code examples
pythonmutagen

Mutagen All File Types


I was given a task to change a function that reads all MP3 files to reading any type of files Mutagen is able to read. Am I suppose to do it by hand? (I.E if .endswith = this or that), or is there a generic way of achieving this?

This is the way the loop looks like:

for root, dirs, files in os.walk("."):
    for filename in files:
        if filename.lower().endswith(".mp3"):
            fullname = os.path.join(root, filename)

Solution

  • supported = ['.mp3','.ogg','.wma'] #put all known here
    
    for root, dirs, files in os.walk("."):
        for filename in files:
            if filename.lower() in supported: #test if filename ext is in supported
                fullname = os.path.join(root, filename)