Skip to content

Instantly share code, notes, and snippets.

@elibroftw
Created April 4, 2022 05:19
Show Gist options
  • Save elibroftw/4599fb62d857bd5dce4040efc46e608d to your computer and use it in GitHub Desktop.
Save elibroftw/4599fb62d857bd5dce4040efc46e608d to your computer and use it in GitHub Desktop.
Python get files of multiple extensions
# abstracted from https://github.com/elibroftw/music-caster/blob/master/src/music_caster.py :: get_audio_uris
import glob
import os.path
from pathlib import Path
MY_EXTS = {'.mp3', '.flac', '.m4a', '.mp4', '.aac', '.mpeg', '.ogg', '.opus', '.wma', '.wav'}
def valid_file_ext(uri) -> bool:
"""
check if uri has a valid MY extension
uri does not have to be a file that exists
"""
return Path(uri).suffix.lower() in MY_EXTS
def get_file_types(directory):
for file in glob.iglob(f'{glob.escape(directory)}/**/*.*', recursive=True):
# might want to check use os.path.file_exists
if os.path.isfile(uri):
if valid_file_ext(file):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment