## dirEntries.py
Version 1.02
## Copyright (c) 2007 Bruce Vaughan, BV
Detailing & Design, Inc.
## All rights reserved.
## NOT FOR
#######################################################################
'''
Version History:
Version 1.01 (
Version 1.02 (
'''
import os
def dirEntries(dir_name, subdir, *args):
'''Return a list of file names found in
directory 'dir_name'
If 'subdir' is True, recursively access subdirectories under 'dir_name'.
Additional arguments, if any, are file
extensions to match filenames. Matched
file names are
added to the list.
If there are no additional arguments, all
files found in the directory are
added to the
list.
Example usage: fileList
= dir_list(r'H:\TEMP', False, 'txt', 'py')
Only files with 'txt' and 'py' extensions will be added to the list.
Example usage: fileList
= dir_list(r'H:\TEMP', True)
All files and all the files in
subdirectories under H:\TEMP will be added
to the list.
'''
fileList
= []
for file in os.listdir(dir_name):
dirfile
= os.path.join(dir_name,
file)
if os.path.isfile(dirfile):
if len(args) == 0:
fileList.append(dirfile)
else:
if os.path.splitext(dirfile)[1][1:]
in args:
fileList.append(dirfile)
# recursively
access file names in subdirectories
elif
os.path.isdir(dirfile) and subdir:
print
"Accessing directory:", dirfile
fileList.extend(dirEntries(dirfile, subdir, *args))
return fileList
if __name__ == '__main__':
#dirName = r'H:\Dxf'
dirName
= r'R:\Music\Ace Of Base'
print '\n'.join(dirEntries(dirName, True, 'xyz')) #'mp3', 'aac',
'm4a'))