Quantcast
Channel: User martineau - Stack Overflow
Viewing all articles
Browse latest Browse all 44

Answer by martineau for How to filter list of items based on substring?

$
0
0

I would suggest using the pathlib module which makes it easy to actually check the file's extension — which is a more rigorous test than merely whether the one string is a substring of another:

from pathlib import Pathfile_paths = ['/Users/me/1. intro.mp4', '/Users/2. intro.vtt', '/Users/1. ppt.rar','/Users/2. ppt.mp4']def filter_on_extension(paths, ext):    return [path for path in paths if Path(path).suffix == ext]file_extension = '.mp4'result = filter_on_extension(file_paths, file_extension)print(result)  # -> ['/Users/me/1. intro.mp4', '/Users/2. ppt.mp4']

Viewing all articles
Browse latest Browse all 44

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>