'Why is file = open("passwordsforatm/"+str(username.get())+".txt","w") not working?

from tkinter import*
from tkinter import messagebox
import os
import string
import random
rand_no = (random.randint(100000000,999999999))

res = ''.join(random.choices(string.ascii_uppercase , k=5))
def mainscreen():
    global screen
    screen = Tk()
    screen.geometry("300x250")
    screen.title("Notes 1.0")
    Label(text = "Notes1.0", bg = "grey",width = "300",height = "2", font = ("calibri", 13)).pack()
    Label(text = "").pack()
    Button(text="Login",height="2",width="30",command=login).pack()
    Label(text="").pack()
    Button(text="Register",height="2",width="30",command=register).pack()
    screen.mainloop()
def scr_del():
    screen.destroy()
def session():
    print("hello its a success")
def register():
    global screen1
    screen1=Tk()
    screen1.title("Register")
    screen1.geometry("300x250")

    global username
    global password
    global username_entry
    global password_entry
    username = StringVar()
    password = StringVar()

    Label(screen1, text="Please enter details below").pack()
    Label(screen1, text="").pack()
    Label(screen1,text="Username: ").pack()
    Entry(screen1,textvariable=username).pack()
    Label(screen1,text="password: ").pack()
    Entry(screen1,textvariable=password).pack()
    Button(screen1,text="Register",width = 10,height =1,command=register_user).pack()
    screen1.mainloop()

def register_user():
    file = open("passwordsforatm/"+str(username.get())+".txt","w")
    file.write(password)
    file.close()

    Label(screen1,text = "Regestration Success", fg="green", font=("calibri", 11)).pack()
    screen1.destroy()
    login()

def login_verify():
    username1 = username_verify.get()
    password1 = password_verify.get()

    list_of_files = os.listdir()
    if username1 in list_of_files:
        file1 = open(username1,"r")
        verify = file1.read().splitlines()
        if password1 in verify:
            messagebox.showinfo("Success", "Login Success")
            session()
        else:
            messagebox.showinfo("Unsuccess", "Wrong Password")

    else:
        messagebox.showinfo("Not Found!!", "User Not Found!!")

def login():
    global screen2
    screen2 = Toplevel(screen)
    screen2.title("Login")
    screen2.geometry("300x250")
    Label(screen2, text="Please enter details below to login").pack()
    Label(screen2, text="").pack()

    global username_verify
    global password_verify
    username_verify = StringVar()
    password_verify = StringVar()

    Label(screen2, text="Username *").pack()
    usernameentry1 = Entry(screen2, textvariable = username_verify)
    usernameentry1.pack()
    Label(screen2, text="").pack()

    Label(screen2, text="Password *").pack()
    passwordentry1 = Entry(screen2, textvariable=password_verify)
    passwordentry1.pack()
    Label(screen2, text="").pack()

    Button(screen2, text = "login", width = 10 , height = 1, command = login_verify).pack()

    screen.mainloop()
    screen2.mainloop()
mainscreen()

This is my code There is some problem in the register_user function

whenever I execute this code there is an error showing

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Ayush\AppData\Local\Programs\Python\Python310\lib\tkinter_init_.py", line 1921, in call return self.func(*args) File "c:\Users\Ayush\Desktop\PycharmProjects\firstprog\python lessons\python projects\atm.py", line 47, in register_user file = open("passwordsforatm/"+str(username)+".txt","w") FileNotFoundError: [Errno 2] No such file or directory: 'passwordsforatm/PY_VAR0.txt'

There is a file called passwordsforatm in the same directory as the project

I have doublechecked the spelling

but still can not make it work

please help



Sources

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

Source: Stack Overflow

Solution Source