Home > Software design >  If Statement depending on the contents of a .txt file - Python 3.9
If Statement depending on the contents of a .txt file - Python 3.9

Time:10-14

def change():
    wind = Tk()...
    text1 = Entry(wind)
    text1.pack()

    def write():
        newfile = open("newfile.txt", "w")
        newfile.write(text1.get())
        newfile.close()

    def show_password():
        global r1
        r1 = open("newfile.txt", "r")
        print("Password is: %s" % r1.read())

    btn2 = Button(wind, text="Show Password", command=show_password)
    btn2.pack(pady=5)

    btn1 = Button(wind, text="Set Password", command=write)
    btn1.pack(pady=5)


def dark():
    pop = Tk()...

    e1 = Entry(pop, background="#292929", foreground="white")
    e1.place(x=60, y=0)
    e2 = Entry(pop, background="#292929", foreground="white")
    e2.place(x=60, y=22)

    def login():
        global no1
        username = (e1.get())
        password = (e2.get())
        print("Username: %s\nPassword: %s" % (e1.get(), e2.get()))
        if username == "a" and password == r1.read():
            new_win = Tk()... 
        elif username == "a" and password != r1.read():
            sleep(5)

mainloop()

Whenever I run the program, I don't get an error message. However, when I type in what I have just set or set before as the password (in the txt file) or what is printed in the terminal, it sleeps the program like I got it wrong. I've posted this before and it was too long, so it's shortened now. This is just revision for my assessment.

CodePudding user response:

For example you can do:

File = open(‘file.txt’, ‘r’)
File_context = File.read()
File.close()

If ‘Word’ in File_context:
    Print(‘FOUND!’)

Tell me if it works!

  • Related