def dir_list5(dir_name, *args):

    fileList = []

    for file in os.listdir(dir_name):

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

        if os.path.isfile(dirfile): # skip directories

            if len(args) == 0:

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

            else:

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

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

    return "".join(fileList)

 

if __name__ == '__main__':

   

    def run_script():

       

        dir_name = (os.path.join('H:\\', 'TEMP', 'temsys'))

        fList = dir_list2(dir_name, 'txt')

        data_file = (os.path.join('H:\\', 'TEMP', 'temsys', 'student_data.dat'))

       

        try:

            fData = open(data_file, 'w')

            print

            for f in fList.split():

               

                ff = open(f, "r")

                print f

                for line in ff:

                    if 'student ID:' in line:

                        # print line[line.index('student ID:')+11:]

                        print line.split('student ID:')[1]

                        fData.write("".join([os.path.basename(f),'----',line.split('student ID:')[1]]))

                        break

                ff.close()

 

            fData.close()

           

        except:

            raise IOError, "Could not open data file or file open error"

       

    #import profile

    #profile.run('run_script()')

    run_script()