Q:

Using a Function From a Module I Built Returns The First Value Despite Calling it Again With Different Arguments

I built the following module for identifying if a file exists in a directory based on its size and name, all so I can use it in a different part of a project. When I try to use the function for the first time, it works great. But when I call it again, different variables with different parameters return the first answer. What am I missing?
Module:
import os
from stat import *
import math

CORRECT_PATH = ''
FLAG = 1

def check_matching(pathname, desired_file_name):
    global FLAG
    if desired_file_name in pathname:
        FLAG = 0
        return pathname

def walktree(dirz, desired_file_name, size):
    global CORRECT_PATH
    global FLAG
    for f in os.listdir(dirz):
        try:
            if FLAG:
                pathname = os.path.join(dirz, f)
                mode = os.stat(pathname)[ST_MODE]
                if S_ISDIR(mode):
                    # It's a directory, recourse into it
                    walktree(pathname, desired_file_name, size)
                elif S_ISREG(mode):
                    # It's a file, call the callback function
                    new_size = int(os.path.getsize(pathname))
                    if (new_size - int(size)) < math.fabs(0.95*int(size)):
                        CORRECT_PATH = check_matching(pathname, desired_file_name)

                else:
                    # Unknown file type, print a message
                    print 'Skipping %s' % pathname

            else:
                try:
                    CORRECT_PATH = CORRECT_PATH.replace('\\', '/')
                    return True, CORRECT_PATH
                except WindowsError as w:
                    #print w
                    if w[0] == 5:
                        return True, CORRECT_PATH
        except WindowsError as e:
            pass
            # print e
            # if e[0] == 5:
            #    return True, CORRECT_PATH  # add correct path now

    return False, ''

Now when I call the this code (This is an example, I'm using two different text files, with different sizes and different names which are saved on my local computer):
import LS_FINAL_FOUR

ans = LS_FINAL_FOUR.walktree("C:/", "a_test", 38)
print ans  # (True, 'C:/a_test.txt')
ans1 = LS_FINAL_FOUR.walktree("C:/", "a_sample", 1000000)
print ans1  # (True, 'C:/a_test.txt')

Both return the same output. Very frustrating. Does anyone know the reason behind this and how to solve it?
Edit: I'm almost certain it's a problem with the module, but I just can't lay my finger on it.

A:

You need to reset the FLAG to 1 each time walktree is called. As it is, the FLAG is set to zero once when you find the first file. Each subsequent call, you re-access this initial result, since if FLAG is false, you run this code again:
else:
  try:
    CORRECT_PATH = CORRECT_PATH.replace('\\', '/')
    return True, CORRECT_PATH

Since CORRECT_PATH is stored as a global, the initial answer persists after your walktree method returns.
I'd suggest fixing up the code by not using globals. Just make check_matching return a result directly to walktree, and don't cache the path result after walktree returns.

