0

My script contains three functions:

get_file() : returns the list of filenames (file_list) with all .xls files in a specific directory

data_conversion() : processes the data contained in the files from file_list

work_flow() : calls get_file() and data_conversion()

def work_flow():
    x = get_file() #returns list of .xls files
    y = [filepath + item for item in x] #add filepath
    counter = 0 #to check how many files are processed
        for file in y:
        try:
            data_conversion()
            counter +=1
        except ValueError:
            pass
    print counter, 'Files processed.'
    print '-------------------------------'
    return()
work_flow()

The problem is as following: If I add the code contained in workflow() without the function to the end of the script, everything runs just fine. However, if I nest it in a function, I get the following error message:

"global variable data_conversion not defined"

Your suggestions are greatly appreciated! Thanks!!!

EDIT: Thank you for the help so far. I checked the code, and the problem appears to be within data_conversion(). If I only include a print function in data_conversion(), everything runs smoothly. So here is the snippet from data_conversion() that seems to be the problem:

def data_conversion():
    print("Executes")
    book = xlrd.open_workbook(file) #LOOKS LIKE THE PROBLEM IS HERE?
    print("Does not execute")

    return()

And here is the code from get_file():

# CREATES A LIST WITH ALL .XLS FILES IN FILEPATH

def get_file():
    path = filepath
    file_list = list()
    listing = os.listdir(path)
    for infile in listing:
        if infile.endswith('.xls'):
            file_list.append(infile)
    return(file_list)

I am quite confident that the answer is close, but I am so stuck...

2
  • 1
    It's unclear without seeing the whole file. Commented Mar 14, 2016 at 11:47
  • I added code from the other functions - this helped me to narrow down the problem. Commented Mar 14, 2016 at 13:23

3 Answers 3

2

I suspect your function data_conversion is defined after you call work_flow. That's why you get this error.

Move the definition of data_conversation above and it will work.

Better yet: do not call functions in the core of your script, but call them at the end, using this:

if __name__ == '__main__':
    work_flow()

This will ensure that all your functions are defined before you call them, and will allow you to import your module from other modules without executing the code.

Sign up to request clarification or add additional context in comments.

1 Comment

Valuable advice - will do so in the future. Thx!
0

Yes, that's right. It can happen if the definition of data_conversation is after the call or you have miss-spelt it's definition.

1 Comment

It appears that the problem is within data_conversion() -> see above
0

I got it - the file never got called by the function! hence:

def data_conversion(file): #instead of ()
    .....
    return()

and

def work_flow():
    .....
    .....
    data_conversion(file) #instead of ()
    .....
    return()

did the trick. Thanks for your help!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.