this can be done with pathlib:
I use same path as script file, but you can substitute any path on line 23
note that I removed the '*' from EXTN pathlib wants just the suffix.
I put two files in my src directory, cows.mpeg and trigger.mp4
I use same path as script file, but you can substitute any path on line 23
note that I removed the '*' from EXTN pathlib wants just the suffix.
I put two files in my src directory, cows.mpeg and trigger.mp4
import pathlib import os # This does all the work: def get_file_list(fpath): EXTN = ['.avi', '.flv', '.mkv', '.mov', '.mp4', '.mpeg', '.mpg', '.webm', '.wmv'] if not isinstance(fpath, pathlib.PosixPath): fpath = (pathlib.Path(fpath)) return [filename for filename in fpath.iterdir() if filename.is_file() and (filename.suffix in EXTN)] def main(): filelist = [] # anchor starting directory to script path os.chdir(os.path.abspath(os.path.dirname(__file__))) #create a pathlib path, here it is current path fpath = pathlib.Path('.') filelist = get_file_list(fpath) for filename in filelist: print(f"{filename.name}") if __name__ == '__main__': main()results:
Output:cows.mpeg
trigger.mp4