如何在Python中搜索文本文件中的某个单词?

如何在Python中搜索文本文件中的某个单词?,python,python-3.x,Python,Python 3.x,我正在使用Python中的函数制作一个登录系统。我想实现一些可以在文本文件(Python)中搜索单词的功能,以便验证输入的数据是否正确。这是我的密码: def have_an_account(): print("Then let's log in!") user_username = input("username:") user_password = input("password:") with o

我正在使用Python中的函数制作一个登录系统。我想实现一些可以在文本文件(Python)中搜索单词的功能,以便验证输入的数据是否正确。这是我的密码:

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
在这里,在用open(“textfile loginsys”,“r”)作为f:之后,我想添加一些东西,如果输入的数据被找到或没有,则在文件中搜索。 我尝试了for循环并使用readlines(),但没有成功。

如何:

打开(r“C:\Users\thatsme\document.txt”,“r”)作为f:
如果f.read()中的“word”:
打印(“word存在于文件中”)
其他:
打印(“word不在文件中…”)

请删除所有不相关的代码(几乎所有内容),并添加“我已经尝试了for循环并使用readlines()”部分。这是您现在需要的,它可以工作了,但为什么我要在说出打开哪个文件之前使用“r”?->
r“C:\Users\thatsme\document.txt”
很高兴听到它成功了。“r”是这样的,因此字符串被视为原始字符串。这将防止像“\t”这样的内容被视为新行,并将保留为文本“\t”。这会给你更多的细节。