Skip to content

Instantly share code, notes, and snippets.

@aahmd
Last active February 24, 2019 03:17
Show Gist options
  • Save aahmd/720d96fa97bb962e6318027159a89407 to your computer and use it in GitHub Desktop.
Save aahmd/720d96fa97bb962e6318027159a89407 to your computer and use it in GitHub Desktop.
comparison of search techniques
import os
from glob import glob
from pathlib import Path
import time
start_path = Path(os.path.expanduser("~"))
"""Glob."""
def list_comp_and_glob():
return [i for i in glob(str(start_path) + '/**/*.mkv', recursive=True)]
"""pathlib.Path + glob."""
def path_lib():
_files = []
pathlist = Path(str(start_path)).glob('**/*.mkv')
for path in pathlist:
path = str(path)
_files.append(path)
return _files
"""standard os.walk; quite quick."""
def os_walk():
_files = []
for root, dirs, files in os.walk(str(start_path)):
for file in files:
if file.endswith(('.mkv', '.avi')) and not file.endswith(('.sample.mkv', '.sample.avi')):
_files.append(os.path.join(root, file))
return _files
""""fn.match + os.walk"""
import fnmatch
def find_files(dir, pattern, pattern_two):
for root, dirs, files in os.walk(start_path):
for basename in files:
if fnmatch.fnmatch(basename, pattern) or fnmatch.fnmatch(basename, pattern_two):
filename = os.path.join(root, basename)
yield filename
def fn_match_walk():
_files = []
for filename in find_files(start_path, '*.mkv', '*.avi'):
_files.append(filename)
return _files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment