Home > Software design >  why python get me this error: filenotfound
why python get me this error: filenotfound

Time:10-12

i have a irrational error, code:

#====================================================#
#X Programming Language 2022(for Jadi) License: GPL  #
#====================================================#
from os import system #importing system func. for cmd

code_location = "" #saving code location in this
code = ""          #saving code in this

def main(): #main func.
    code_location = input() #get code location from user
    code = get_code() #cal get_code and save res to code var
    print(code) #print code
    end() #calling end

def get_code(): #get_code func. for getting code from file
    code_file = open(code_location, 'r') #openning file with method read and saving to code_file
    res = code_file.readlines() #saving the content from file to res
    return res #returning res

def compiler(): #compiler func. for compiling code
    pass

def end(): #when program end...
    input("Press Enter to Countinue...")

if __name__ == "__main__":
    main()

and this is code directory: enter image description here

running:

enter image description here

CodePudding user response:

Short answer: Your two code_location variables are not the same thing.

Variable scopes

Variables have a property called a scope, which is essentially which context they belong to. Unless specified otherwise, variables within a function exist only within that function. Consider:

a = 0

def set_a():
    a = 1

set_a()
print(a)

This will print 0. This is because the a variable within the function set_a is actually a different variable to the a defined in line 1. Although they have the same name, they point to different places in memory.

Solutions

There are a few ways to do this:

Defining scope

Either, you can set the scope of a within the function to global (instead of local). What this does is now, instead of a within the function pointing to a different memory location, it points to the same memory location as a outside the variable. This is done like so:

a = 0

def set_a():
    global a
    a = 1

set_a()
print(a)

In this case, a will be set to 1, and "1" will be printed

Passing as an argument

Another way to do this, and may be more relevant in your circumstance, is to pass the value as a variable to the function. In your case, you are using code_location as the file path, so therefore what you want to pass code_location into the function. You would then have to define your function like this:

def get_code(code_location):

and call the function (from your main function) like this:

code = get_code(code_location)

Notes

When operating on files, it is best practice to use a with block. This handles closing your file when you are done with it, and can prevent corruption of files in the rare case that something goes wrong with your code. This can be done like this:

with open(code_location, 'r') as code_file:
    res = code_file.readlines()
return res

CodePudding user response:

Python global variables are read-only in local scopes (e.g. your function's scope) by default. So in the line code_location = input() you are essentially creating a new new local variable of the same name and assigning the input to it.

In order to write to your global variable instead, you first have to declare your intention:

def main(): #main func.
    global code_location # DECLARE WRITE INTENTION FOR GLOBAL
    code_location = input() #get code location from user
    code = get_code() #cal get_code and save res to code var
    print(code) #print code
    end() #calling end

You don't have to do the same thing in get_code() since you are only reading from code_location there.

PS: As was alluded to in the comment, it's good practice to close opened files after you've finished with them:

def get_code(): #get_code func. for getting code from file
    code_file = open(code_location, 'r') #openning file with method read and saving to code_file
    res = code_file.readlines() #saving the content from file to res
    code_file.close() # CLOSE FILE
    return res #returning res

Or have it done automatically by a context manager:

def get_code(): #get_code func. for getting code from file
    with open(code_location, 'r') as code_file: #openning file with method read and saving to code_file
        res = code_file.readlines() #saving the content from file to res
        return res #returning res
  • Related