Skip to content

Instantly share code, notes, and snippets.

@tgarc
Created February 2, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgarc/45fcdee4597dc1fc724b135f76fea3ba to your computer and use it in GitHub Desktop.
Save tgarc/45fcdee4597dc1fc724b135f76fea3ba to your computer and use it in GitHub Desktop.
python file finder
import os
import glob
import errno
try:
from itertools import ifilter as filter
except ImportError:
pass
def findfile(filepath, find_all, search_path='', recursive=False, allowglob=False, rel_path='.', follow_links=False):
filelist = []
# first expand user; we want user relative paths to be considered
# same as absolute paths
filepath = os.path.expanduser(filepath)
# For absolute paths, add any existing matches
if os.path.isabs(filepath):
if allowglob:
filelist.extend(glob.iglob(filepath))
else:
filelist.append(filepath)
# This is an 'explicity' relative path, consider it relative to rel_path
elif os.path.dirname(filepath) != '':
filepath = os.path.join(os.path.abspath(rel_path), filepath)
if allowglob:
filelist.extend(glob.iglob(filepath))
else:
filelist.append(filepath)
# This has no explicit relative path component, so we can search
# for this on the search_path
elif search_path:
for pathcmp in search_path.split(os.path.pathsep):
for root, dirs, files in os.walk(pathcmp, followlinks=follow_links):
root = os.path.abspath(root)
if allowglob:
filelist.extend(glob.iglob(os.path.join(root, filepath)))
else:
pathmatch = os.path.join(root, filepath)
if not find_all and os.path.isfile(pathmatch):
return pathmatch # for speed's sake, return here
filelist.append(pathmatch)
if not recursive: break
# filter out non-existent and non-file matches
filelist = list(filter(os.path.isfile, filelist))
# For convenience, we have the somewhat odd behavior that when
# searching for a single match (find_all=False) we raise an error
# when no match is found. OTOH, we stay silent if we're returning
# a list of matches
if not (find_all or filelist):
raise IOError(errno.ENOENT, "No match found for expression: '%s'" % filepath)
return filelist if find_all else filelist[0]
@JessicaLawson
Copy link

Hello
Happy to meet you, my name is Miss Jessica Lawson, it is my pressure to meet you here  today through this site, i will like you to write to me so that i can be able to tell you more about me and the reason of my contact with you. here is my private email address, please write to me here so that i can tell you more anout me and also to send my picture  to you ( jesicalawson91@gmail.com )  thanks

Yours Miss Jessica Lawson

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment