-4

I'm making a login system using functions in Python. I want to implement something that can search for a word in a text file (in Python), so I could verify if the inputted data is correct or not. Here is my code:

def have_an_account():
    print("Then let's log in!") 
    user_username = input("username:")
    user_password = input("password:")
    with open("textfile loginsys", "r") as f:
        pass

Here, after with open("textfile loginsys", "r") as f: I want to add something that search in the file if the inputted data is found back or not. I have tried a for-loop and using readlines(), but without success.

  • 3
    Please **remove** all irrelevant code (pretty much everything) and add the "I have tried a for-loop and using readlines()" part. – DYZ Jun 27 '20 at 21:19
  • Here is what you need https://stackoverflow.com/a/46739016/13782669 – alex_noname Jun 27 '20 at 21:23

1 Answers1

0

How about:

with open(r"C:\Users\thatsme\document.txt", "r") as f:
    if "word" in f.read():
        print("word exists in file")
    else:
        print("word not in file...")
  • Now it works, but why should I use 'r' before saying which file I open? ->`r"C:\Users\thatsme\document.txt"` – check les specs Jun 29 '20 at 20:06
  • Glad to hear that it worked. The "r" is so that the string is treated as a raw string. This will prevent something like a "\t" from being treated as a new line and will keep as a literal "\t" instead. This will give you more details https://www.journaldev.com/23598/python-raw-string. – Bernardo Trindade Jun 30 '20 at 01:28