Python - Print a value without intterupting a loop/function
Question: Using Python 2.7 on Windows 7.

I has a recursive function, to find a file in the path, also looking in the
sub-folders.

**My Problem:**  
During long searches, I wish to see the value of `retval` on
`KeyboardInterrupt`.  
But the code is raising an error on `KeyboardInterrupt`, as it is interrupting
a "deeper" function call/run.

**My Question:**  
Is there any way to not interrupt the loop, but still have the values printed,  
Or  
A different way to do the same (that is not using `KeyboardInterrupt` but
something else)

**My Code:**

    
    
    import os
    
    def search(path,filename):
        try:
            global found
            folders = []
            retval = []
    
            try:    
                for item in os.listdir(path):
                    if not os.path.isfile(os.path.join(path, item)):
                        folders.append(os.path.join(path, item))
                    else:
                        if item == filename:
                            found += 1
                            retval.append(os.path.join(path, item))
            except WindowsError,e:
                print str(e)[10:]
    
            for folder in folders:
                retval += search(folder,filename)
            return retval
        except KeyboardInterrupt:
            print retval
    
    found = 0
    path = 'C:\\'
    filename = 'test.txt'
    print search(path,filename)
    


Answer: You can add the `KeyboardInterrupt` handler lower in the hierarchy:

    
    
    for folder in folders:
            try:
                 retval += search(folder,filename)
            except KeyboardInterrupt:
                   print retval
    return retval
    

You could test that, although I'm not quite sure how that handles inside a
loop like the one you have.

