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