Search code examples
pythonpython-3.xtkintertkinter-canvastkinter-entry

how to search if user input file exist or not in tkinter python


i have made that when ever someone reg the txt file is created and named on user username. now im trying to check if username txt file is there in dir or not here is the code i tried . im unable to check if username txt file is there . i tried many diff way searched google but no luck some i have mentioned as comment. pls help looking

 def db():
        a = (E1.get(), E2.get(), E3.get(),E4.get(),E5.get())
        
        if "./library/"+E1.get() == os.path.isfile("./library/"+E1.get()):
        # if E1.get() == os.path.isfile(E1.get()):
        # if E1.get()in os.path.isdir("./library/"):

            cur.execute("insert into Booking values(?,?,?,?,?)", a)
            con.commit()
            cur.execute("select * from Booking")
            a = cur.fetchall()
            ab = int(E4.get())
            cost=100
            ac = ab * cost
            messagebox.showinfo("Congratulation!!", "Your oreder value is %s" % ac)
            print(a)

            te.destroy()
            os.system("library.py")
        else:
            print("username invalid")

Solution

  • Try replacing the below line

    if "./library/"+E1.get() == os.path.isfile("./library/"+E1.get()):
    

    with

    if os.path.isfile("./library/"+E1.get()):
    

    You probably are confused with the return value of os.path.isfile() fn.