'I am getting Name Error in else statement

So! I made a simple program that gets a file name as input and open it and read it. and there is a special file name "mbox.txt", to open it user must insert the pin code, after that user can access the content of the file.

for this I made a if else condition, if fname=="mbox.txt": insert pass else: open it normally,

and yes! I use try,except and finally, if the file does not find execute runs the except code otherwise continue with finally.

when ever I insert unknown file name it runs the except code and gives the NameError.

fname=input("Enter the file name: ")
try:
   open_file=open(fname)
except:
 print("The File'",fname,"'Did not find Please Enter A Existing File")
finally:
   y=3
if fname=="mbox.txt": 
  while y>0:
    print("To Open This File You Need To Insert The Access Passward")
    line=int(input("Insert passward"))
    if line==8523:
       read= open_file.read()
       print(read)  
       break
    else:
        if y==3 :
            print("Access Denied!")           
            y=y-1
            continue
        elif y==2:
           print("Access Denied!")   
           y=y-1
           continue
        else:
           print("UNKNOWN USER DETECTED, THE PROGRAM TERMINATE IMMIDIATELY")   
           break
         
else:
 read= open_file.read()
 print(read)

error

Traceback (most recent call last):
  File "c:\Users\TRED WINGS\Desktop\py\tempCodeRunnerFile.py", line 30, in <module>
    read= open_file.read()
NameError: name 'open_file' is not defined


Solution 1:[1]

I got so caught up in app registrations and tokens, I completely forgot that I also have a server admin account for the databaser server.. Stupid me...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 CJ Scholten