in scripts/yapl/Utilities.py [0:0]
def listFilesOfType(pathName,extension):
"""
Return a Jython list that contains either a single full path to a file or a list of
full paths to files. If the given pathName is a file, then it is checked to confirm
that the file has the given extension. If the given pathName is a directory, then
a listing of that directory is taken and a sorted list is returned with all the file
paths for all of the files in that directory with the given extension.
NOTE: extension may or may not include the the leading dot character, i.e.,
it is ok to pass in .json or json. If the dot is not present it will be added.
The os.path.splitext() method includes the dot character.
NOTE: If extension is None it is converted to the empty string and files with
no extension are returned in a list. Extension may also be passed in as the
empty string to achieve the same effect.
This method is convenient for use in a top level script where a given argument may
represent a path a single file for for processing, or a directory with one or more
files for processing by that script.
The listFilesOfType does not descend the directory tree rooted at pathName when
pathName is a directory. It only looks at files in the top level of the directory
referenced by pathName. <TBD>Are there use cases for doing a recursive descent?
"""
if (not pathName):
raise Exception("The path name to a directory or file (pathName) cannot be empty or None.")
#endIf
if (extension == None): extension = ''
if (extension):
if (extension[0] != '.'):
extension = ".%s" % extension
#endIf
#endIf
filePaths = []
if (os.path.isfile(pathName)):
ext = os.path.splitext(pathName)[1]
if (ext == extension):
filePaths.append(pathName)
#endIf
elif (os.path.isdir(pathName)):
# Note os.listdir() result is not sorted
dirListing = os.listdir(pathName)
for x in dirListing:
xpath = os.path.join(pathName,x)
if (os.path.isfile(xpath)):
ext = os.path.splitext(xpath)[1]
if (ext == extension):
filePaths.append(xpath)
#endIf
#endIf
#endFor
else:
raise Exception("Unexpected type for pathName: '%s'. Expected file or directory." % pathName)
#endIf
filePaths.sort()
return filePaths