how can I save the output of a search for files matching *.txt to a variable?
Question: I'm fairly new to python. I'd like to save the text that is printed by at this
script as a variable. (The variable is meant to be written to a file later, if
that matters.) How can I do that?

    
    
    import fnmatch
    import os
    
    for file in os.listdir("/Users/x/y"):
        if fnmatch.fnmatch(file, '*.txt'):
            print(file)
    


Answer: You can store it in a list:

    
    
    import fnmatch
    import os
    
    matches = []
    for file in os.listdir("/Users/x/y"):
        if fnmatch.fnmatch(file, '*.txt'):
            matches.append(file)
    

