Monday, 27 September 2021

Check if a file type is a media file?

I am trying to loop through a list of files, and return those files that are media files (images, video, gif, audio, etc.).

Seeing as there are a lot of media types, is there a library or perhaps better way to check this, than listing all types then checking a file against that list?

Here's what I'm doing so far:

import os
types = [".mp3", ".mpeg", ".gif", ".jpg", ".jpeg"]
files = ["test.mp3", "test.tmp", "filename.mpg", ".AutoConfig"]

media_files = []
for file in files:
    root, extention = os.path.splitext(file)
    print(extention)
    if extention in types:
        media_files.append(file)

print("Found media files are:")
print(media_files)

But note it didn't include filename.mpg, since I forgot to put .mpg in my types list. (Or, more likely, I didn't expect that list to include a .mpg file, so didn't think to list it out.)



from Check if a file type is a media file?

No comments:

Post a Comment