Printing the output that is a list of file names to a text file
Question: I have made a Python program that checks the files in a folder and returns a
list of the ones that end with .txt. Now i want to output that to a file. Here
is my code:

    
    
    import os
    import os.path
    import sys
    
    f=open("E:\\test.txt",'w')
    f.write('')
    
    path=os.path.abspath("E:\\test")
    def print_result():
        print(name)
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith(".txt"):
                print_result()
    
    Version 1:
    
    alist=[print_name]            
    alist.append(print_result)
    f=open("E:\\test.txt", 'a')
    f.writelines(alist)
    
    Version 2:
    
    alist=name
    alist.append(name)
    f = open("E:\\test.txt",'a')
    print (name)
    
    Version 3:
    
    frame=print_result
    saveout = sys.stdout
    fsock = open("E:\\test.txt", 'a')
    sys.stdout = fsock
    print (frame)
    sys.stdout = saveout`enter code here`
    
    As you can see i have tried diferent types. The thing is that i only get the following in the file, instead of the actual output list:
    
    <function print_result at 0x004F6DB0>
    


Answer: This can be done a little simpler using a
[glob](https://docs.python.org/2/library/glob.html):

    
    
    import glob
    
    txtfiles = glob.glob('E:\\test\\*.txt')
    with open('E:\\test.txt', 'w') as f:
        for txtfile in txtfiles:
            f.write('{}\n'.format(txtfile))
    

