Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6682321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:41:33+00:00 2026-05-26T04:41:33+00:00

I wrote the below code in order to check for three files and whichever

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:41:33+00:00Added an answer on May 26, 2026 at 4:41 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new in programming .I wrote the below code in order to prompt
I wrote the following Perl script (below) in order to create simple XML file.
I wrote the below code in order to explain my issue. If I comment
I wrote the program below in order to read every line of the file
I wrote the code below : 1. MyClass[] origArr=new MyClass[3]; 2. MyClass[] arr1; 3.
I write the code below to delete a file: FileInfo file = new FileInfo(filename);
I wrote below code to compare to arrays that have same elements but in
I wrote a bubble sort in Prolog (code below). It works but it smells.
I wrote code below that is very similar to the accepted answer here .
In order to list pathes in Windows,I wrote below Perl function(executed under StrawBerry runtime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.