import os

from time import sleep

 

# String concatenation is ineffieicent in Python

"""

def dir_list2(dir_name, *args):

    fileStr = ""

    for file in os.listdir(dir_name):

        dirfile = os.path.join(dir_name, file)

        if os.path.isfile(dirfile):

            if len(args) == 0:

                fileStr += dirfile + '\n'

            else:

                if os.path.splitext(dirfile)[1][1:] in args:

                    fileStr += dirfile + '\n'

        elif os.path.isdir(dirfile):

            # print "Accessing directory:", dirfile

            fileStr += dir_list2(dirfile, *args)

    return fileStr

"""

# Joined string

def dir_list2(dir_name, *args):

    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('%s\n' %(dirfile))

            else:

                if os.path.splitext(dirfile)[1][1:] in args:

                    fileList.append('%s\n' %(dirfile))

        elif os.path.isdir(dirfile):

            # print "Accessing directory:", dirfile

            fileList += dir_list2(dirfile, *args)

    return "".join(fileList)

 

if __name__ == '__main__':

   

    def run_script():

        dir_name = (os.path.join('C:\\', 'SDS2_7.0', 'macro'))

        f = dir_list2(dir_name, 'py')

        print f

        """

        fList = f.split('\n')

        sleep(2)

        for s in fList:

            print s

        """

 

    import profile

    profile.run('run_script()')