0

I wrote the below code in order to check for three files and whichever files exist, run a "scan" on the file (if a file does not exist, don't worry about it just run a "scan" on the available files) and produce the proper output file on those available files.

The program I'm working on includes the following code:

def InputScanAnswer():
    scan_number = raw_input("Enter Scan Type number: ")
    return scan_number

This function checks if these three files exist and if so, assign specific values to hashcolumn and to filepathNum

def chkifexists():
    list = ['file1.csv', 'file2.csv', 'file3.csv']
    for filename in list:
        if os.path.isfile(filename):
            if filename == "file1.csv":
                hashcolumn = 7
                filepathNum = 5
            if filename == "file2.csv":
                hashcolumn = 15
                filepathNum = 5
            if filename == "file3.csv":
                hashcolumn = 1
                filepathNum = 0
            #print filename, hashcolumn, filepathNum


def ScanChoice(scan_number):
    if scan_number == "1":
        chkifexists()
        onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) #this is what is giving me errors...
    elif scan_number == "2":
        print "this is scan #2"
    elif scan_number =="3":
        print "this is scan #3"
    else:
        print "Oops! Invalid selection. Please try again."


def onlinescan(FileToScan, ResultsFile, hashcolumn, filepathNum):
    # web scraping stuff is done in this function

The error that I run into is global name 'filename' is not defined. I realize that the problem is I'm attempting to send local variables from chkifexists() to the onlinescan() parameters. I tried using

return filename
return hashcolumn
return filepathNum

at the end of the chkifexists() function but that was not working either. Is there anyway to do what I'm trying to do in the

onlinescan(filename, filename + "_Online_Scan_Results.csv", hashcolumn, filepathNum) 

line without using global variables? I know they are discouraged and I'm hoping I can go about it another way. Also, does having hashcolumn and filepathNum parameters in onlinescan() have anything to do with this?

1 Answer 1

4

Inside chkifexists, you would return all three variables like so:

return (filename, hashcolumn, filepathNum)

You would retrieve these by calling the function like so:

(filename, hashcolumn, filepathNum) = chkifexists()

You now have them in your function scope without needing global variables!

Technically, you don't need the parenthesis, either. In fact, I'm not sure why I included them. But it works either way, so what the heck.

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

4 Comments

What about the order? Can it also be like this: chkifexists() = filename, hashcolumn, filepathNum ?
That's easy enough to try in your interpreter, but since I'm feeling generous, I'll give you the answer: no, you get a SyntaxError: can't assign to function call. Which, when you think about it for a secoond, makes a lot of sense.
Good answer. I think the parenthesis convey that you're not returning three things, you're returning one thing -- a tuple -- which you pack three variables into on the fly and unpack again on the fly on the other side.
@agf - that is true. Leaving off the parenthesis is sort of just sugar; you're passing a tuple either way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.