findfiler.py
Tilbage
Hent filen
Licens GPL
Find filer
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
u"""Usage: findfiler.py [-p <path>] [-s <suffix> ] [-S <IgnoresSuffix>] [-D <IgnoreDir>]
<path>: default to "." angiver søgnings startpunkt
<IgnoresSuffix>: Suffix to ignore defaults to %(isuffix)s
<suffix>: Suffix to find defaults to %(suffix)s
<IgnoreDir>: dirs to ignore
Returns a list off filenames
"""
import sys
import os
import optparse
from os.path import isfile
def find(Path=".", onlySuffix=[], IgnoreSuffix=[], IgnoreDirs=[], vim="" ):
"""find [<path>] [<IgnoresSuffix>] [<IgnoreDir>]
<path>: default to "." angiver søgnings startpunkt
<onlySuffix> : only this suffix
<IgnoresSuffix> : Sufix to ignore defalt to []
<IgnoreDir> : dirs to ignore defalt to []
<vim> : drop files ending with this string
Returns a list off filenames ignoring pipes.
"""
Foundnames = []
for dirpath, dirnames, filenames in os.walk( Path ):
Idirs = [ Dir for Dir in dirpath.split("/") if Dir in IgnoreDirs ]
if Idirs:
continue
for fname in ["%s/%s" % (dirpath, fname ) for fname in filenames]:
if not isfile( fname ):
continue
suffix=fname.split(".")[-1]
if vim and fname.endswith(vim): continue
if onlySuffix:
if suffix not in onlySuffix:
continue
elif suffix in IgnoreSuffix:
continue
yield "%(fname)s" % vars()
if __name__ == "__main__":
suffix = []
isuffix = []
P = optparse.OptionParser(usage=__doc__ % vars())
P.add_option("-p","--path",
help="path to where the search begins",
action="store",
default="." )
P.add_option("-s","--suffix",
help="files ending with suffix",
action="append",
default=suffix)
P.add_option("-t","--test",
help="start pdb, hackers choice",
action="store_true",
default=False)
P.add_option("-i","--isuffix",
help="ignore files ending with suffix",
action="append",
default = isuffix)
P.add_option("-d","--dirs",
help="ignore dirs",
action="store_true",
default=[])
(optlist, args) = P.parse_args(sys.argv[1:])
if optlist.test:
import pdb;pdb.set_trace()
print " ".join(find(Path=optlist.path, onlySuffix=optlist.suffix, IgnoreSuffix=optlist.isuffix, IgnoreDirs=optlist.dirs))