Johannes Schwaninger
Johannes Schwaninger

Reputation: 221

Calling a function from within a function in Python

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...

Upvotes: 0

Views: 163

Answers (3)

Johannes Schwaninger
Johannes Schwaninger

Reputation: 221

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!

Upvotes: 0

Vishal Yarabandi
Vishal Yarabandi

Reputation: 457

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.

Upvotes: 0

DevShark
DevShark

Reputation: 9112

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.

Upvotes: 2

Related Questions